Arsalan Habib
Arsalan Habib

Reputation: 1395

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.

Upvotes: 7

Views: 4791

Answers (4)

feca
feca

Reputation: 1169

I had this problem with only iOS7. iOS8 works perfect in generally. The solution is that the height of toolbar is 44.0, and I setted the delegate UIBarPositioning, and the items position to top:

- (UIBarPosition) positionForBar: (id<UIBarPositioning>) bar {
    return UIBarPositionTop;
}

Upvotes: 0

Josip B.
Josip B.

Reputation: 2464

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;

Upvotes: 4

Bond007
Bond007

Reputation: 51

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.

Upvotes: 4

Arsalan Habib
Arsalan Habib

Reputation: 1395

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]];

Upvotes: 5

Related Questions