How to align UIBarButtonItem in UIToolbar in iOS universal app? How to align UIBarButtonItem in UIToolbar in iOS universal app? xcode xcode

How to align UIBarButtonItem in UIToolbar in iOS universal app?


You can do it all in your Storyboard.

Select a Bar Button Item in the Object library and drag it into your Toolbar. Add a Flexible Space Bar Button Item at its right. Add your second Bar Button Item at its right. Then, add a second Flexible Space Bar Button Item at its right.

The result will look like this in Interface Builder:

enter image description here

If necessary, you can add an extra Fixed Space Bar Button Item at the right edge of your Toolbar to be sure that your second Bar Button Item is really centered.

User lxt gives another example of this in answer for a similar question here.

Edit: Be sure that your UIToolbar has good constraints before proceeding!


you can do this programmatically

UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"item1" style:UIBarButtonItemStylePlain target:self action:nil];UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"item2" style:UIBarButtonItemStylePlain target:self action:nil];UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithTitle:@"item3" style:UIBarButtonItemStylePlain target:self action:nil];self.toolbarItems = [NSArray arrayWithObjects: item1, flexible, item2, flexible, item3, nil];

check this link if you want more details

How to place UIBarButtonItem on the right side of a UIToolbar?


I think you should try Flexible bar button space item and for more understating i am sharing a link, so have look of it might this help you out

UIToolbar and Flexable/Fixed bar button items