Incorrect upright position for UIBarButtonItems in UIToolbar for iOS 7

I have this piece of code for an iPad app that works great 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:

enter image description here

The same code when running on iOS 7 produces this result:

enter image description here

For some reason, the buttons move to the bottom of the toolbar in iOS 7.

Now I can move them using the UIBarItem imageInset property, but this seems like a hack, because then I need to check the iOS version and only make the image if the iPad is running iOS 7+. My question is: did I miss something specific for iOS 7 related to UIToolbar? I switched to iOS 7 UI Transition Guide and did not find anything specific for this problem.

+6
source share
4 answers

Since I did not get any other answers and found the right solution for me, I am going to answer this question if someone else encounters the same problem. If your goal is iOS 5.0 and above, thereโ€™s a convenient way to add multiple items to the right pane. Here's the fix:

 [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:deleteButton, bi, nil]]; 
+5
source

Check your toolbar in the designer (storyboard). Make sure you have a width for all the buttons on the toolbar. I had a similar problem, and after I determined the width for each button on the toolbar in the storyboard, it went away and now the buttons are correctly located on the toolbar in iOS 7.

+5
source

Creating a UIToolbar with CGRectZero and setting its 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; 
+4
source

I had this problem only with iOS7. iOS8 works great overall. The solution is that the height of the toolbar is 44.0, and I configured the UIBarPositioning delegate, and the position of the position is up:

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

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


All Articles