Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Edit Empty Spaces of Left, Right UIBarButtonItem in UINavigationBar [iOS 7]

I was using iOS 6.1 earlier, but now I have moved to iOS 7. Along with other problems, I have observed that in my navigation bar, the left space of left bar button item and right empty space of the right button bar item are quite more in IOS 7 than in iOS 6.

I need to know is there a way I can reduce empty spaces of left, right bar button items in navigation bar??

Thanks in advance.

like image 687
Salman Zaidi Avatar asked Sep 20 '13 10:09

Salman Zaidi


2 Answers

I was also facing this problem. I also have feelings that in iOS 7 there is more space. And I figured out that this is about 10 points more. I usually use negative spaces when I want for LeftBarItemButton to start from the edge. This can be useful for you as well.

UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];  negativeSpacer.width = -16; // it was -6 in iOS 6  [self.navigationItem setLeftBarButtonItems:@[negativeSpacer, requiredButton]; /* this will be the button which you actually need */] animated:NO]; 
like image 97
Adnan Aftab Avatar answered Sep 17 '22 13:09

Adnan Aftab


Based on @C_X his answer I've created a category which adds and positions the UIBarButtonItem based on the iOS version of the current device.

// UINavigationItem+Additions.h @interface UINavigationItem (Additions) - (void)addLeftBarButtonItem:(UIBarButtonItem *)leftBarButtonItem; - (void)addRightBarButtonItem:(UIBarButtonItem *)rightBarButtonItem; @end  // UINavigationItem+Additions.m @implementation UINavigationItem (Additions)  - (void)addLeftBarButtonItem:(UIBarButtonItem *)leftBarButtonItem {     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {         // Add a negative spacer on iOS >= 7.0         UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace                               target:nil action:nil];         negativeSpacer.width = -10;         [self setLeftBarButtonItems:[NSArray arrayWithObjects:negativeSpacer, leftBarButtonItem, nil]];     } else {         // Just set the UIBarButtonItem as you would normally         [self setLeftBarButtonItem:leftBarButtonItem];     } }  - (void)addRightBarButtonItem:(UIBarButtonItem *)rightBarButtonItem {     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {         // Add a negative spacer on iOS >= 7.0         UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]                                        initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace                                        target:nil action:nil];         negativeSpacer.width = -10;         [self setRightBarButtonItems:[NSArray arrayWithObjects:negativeSpacer, rightBarButtonItem, nil]];     } else {         // Just set the UIBarButtonItem as you would normally         [self setRightBarButtonItem:rightBarButtonItem];     } }  @end 

In your view controller you can now use [self.navigationItem addLeftBarButtonItem:leftBarButtonItem]; and [self.navigationItem addRightBarButtonItem:rightBarButtonItem];

I've also tried subclassing UIButton and override -alignmentRectInsets but this gave me problems with transitions between views.

like image 26
smek Avatar answered Sep 17 '22 13:09

smek