Reduce blank space between bar button items in UINavigationBar
Asked Answered
D

2

7

I notice the blank space between bar button items is quite large. I want to reduce the space to have more room for my title. I tried to create fixed space then added it among the buttons but it didn't work. Does anybody know how to do it?

UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    fixedItem.width = 10.0f;
self.navigationItem.rightBarButtonItems = @[settingsButtonItem, fixedItem, speakerButtonItem, fixedItem, favouriteButtonItem];
Dita answered 13/6, 2014 at 17:48 Comment(0)
P
1

You're close but what you need is a negative fixed space. If your other UIBarButtonItems use custom views, check your frame for those views. Here's an example of adding two right bar button items that hug the edge more. In your case you'll want to add negative space between the buttons as well.

UIBarButtonItem *negativeSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpace.width = -8;

UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 44, 44);
[backButton setImage:[[UIImage imageNamed:@"ic_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]
            forState:UIControlStateNormal];
backButton.imageView.tintColor = [UIColor whiteColor];
[backButton addTarget:self
               action:@selector(backPressed)
     forControlEvents:UIControlEventTouchUpInside];

self.backNavButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.backNavButton.enabled = NO;

UIButton *forwardButton = [UIButton buttonWithType:UIButtonTypeCustom];
forwardButton.frame = CGRectMake(0, 0, 44, 44);
[forwardButton setImage:[forwardImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]
               forState:UIControlStateNormal];
forwardButton.imageView.tintColor = [UIColor whiteColor];
[forwardButton addTarget:self
                  action:@selector(forwardPressed)
        forControlEvents:UIControlEventTouchUpInside];

self.forwardNavButton = [[UIBarButtonItem alloc] initWithCustomView:forwardButton];
self.forwardNavButton.enabled = NO;

self.navigationItem.rightBarButtonItems = @[negativeSpace,self.forwardNavButton,self.backNavButton];
Polley answered 8/4, 2015 at 19:49 Comment(1)
Thank you so much! The trick was to first set UIButtons with their frames, and only then set the corresponding UIBarButtons. I had been struggling so much with this.Replica
W
1

Using Xcode 6.4, This worked beautifully for me with 1 line of code!
https://mcmap.net/q/206513/-ios7-excessive-navigationbar-button-padding

self.myBarButtonItem.imageInsets = UIEdgeInsetsMake(0, 25, 0, -25);

Note- Unfortunately this does not move the hit area of the button, only its image. But a good solution if you just need to move the button a little bit!

Walkway answered 31/7, 2015 at 16:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.