Set UIBarButtonItem Programmatically

How can I set the position of a UIBarButtonItem? For example, I would like to install it both to the right of the UIToolbar, and the left one depending on the state.

Thanks.

+4
source share
2 answers

You do not set the direct position of the UIBarButtonItem in the UIToolbar . Instead, you determined the order of the elements and put the flexible space on the left or right.

What can you do:

  • Create the UIBarButtonItem that you want to place (button 1).
  • Create a UIBarButtonItem of type UIBarButtonSystemItemFlexibleSpace (button 2).
  • If you want to place the button on the left, create an array using (button 1) and (button 2) and pass it to UIToolbar using the setItems:animated: method.
  • If you want to place the button on the right, create an array using (button 2) and (button 1) and pass it to UIToolbar using the setItems:animated: method.
+18
source

Hope this helps you ...

 UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init]; [keyboardDoneButtonView sizeToFit]; UIBarButtonItem* PrevButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:105 target:nil action:nil]; //< UIBarButtonItem* NextButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:106 target:nil action:nil]; //> UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(doneClicked:)]; UIBarButtonItem* flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem *fake = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil] ; [keyboardDoneButtonView setItems:[NSArray arrayWithObjects: PrevButton,fake, NextButton,fake,flexSpace,fake,doneButton,nil] animated:YES]; 

The link explains the Button Bar Element. Methods and Arguments https: //developer.apple.com/library ..... Use the Fake element to get the exact location of the pinch on the button ...

+1
source

Source: https://habr.com/ru/post/1307829/


All Articles