Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect vertical position for UIBarButtonItems in UIToolbar for iOS 7

I have this piece of code for an iPad application that works fine for any iOS below iOS 7

UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 75, 44)];

NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:2];

UIBarButtonItem *composeButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(toggleDelete:)];

[buttons addObject:composeButton];

UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];

fixedSpace.width = 5;

[buttons addObject:fixedSpace];

UIBarButtonItem* bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(touchMe:)];

[buttons addObject:bi];

[tools setItems:buttons animated:NO];

tools.barStyle = -1;

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];

[bi release];
[fixedSpace release];
[composeButton release];
[buttons release];    
[tools release];

The result of this pre iOS 7 is:

enter image description here

The same code when run on iOS 7 yeilds this result:

enter image description here

For some reason the buttons are moved to the bottom of the Toolbar in iOS 7.

Now, I can reposition them using the UIBarItem imageInset property but that seems to be kind of hackish, because then I need to check for iOS version and only do the imageInset if the iPad's running iOS 7+. My question is am I missing anything specific to iOS 7 pertaining to UIToolbar? I went over the iOS 7 UI Transition Guide and cannot find anything specific to this problem.

like image 725
Arsalan Habib Avatar asked Oct 15 '13 03:10

Arsalan Habib


3 Answers

Since I did not get any other answers and found the right fix for me, I'm going to answer this question for anyone else running into the same problem. If your target is iOS 5.0 and above, there is a convenient method to add multiple items to the right bar button. Here's the fix:

[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:deleteButton, bi, nil]];
like image 145
Arsalan Habib Avatar answered Sep 28 '22 00:09

Arsalan Habib


Check your toolbar in the designer (storyboard). Make sure you have width specified for ALL buttons on the toolbar. I had similar issue and after I have specified width for each button in the toolbar in the storyboard it has gone away and now buttons are properly positioned on the toolbar in iOS 7.

like image 43
Bond007 Avatar answered Sep 28 '22 02:09

Bond007


Creating UIToolbar with CGRectZero and setting it's frame after setItems: solved my problem on iOS 7.

UIToolbar *tools = [[UIToolbar alloc] initWithFrame:CGRectZero];
//Create array with items
[tools setItems:buttonsArray animated:NO];
//Setting frame at this moment fixes the issue    
tools.frame = toolbarFrame;
like image 39
Josip B. Avatar answered Sep 28 '22 01:09

Josip B.