Change the height of NavigationBar and UIBarButtonItem elements inside it in Cocoa Touch

mgamer picture mgamer · Nov 27, 2011 · Viewed 21.9k times · Source

I suppose it's not strictly in line with Apple guidelines but I guess it must be possible somehow. I'd like to change the height of navigation bar inside UINavigationController and the height of UIBarButtonItem elements inside that bar.

Using a trick from this question I managed to change the height of navigation bar but I can see no way of adjusting the height of bar button items.

If anyone knows how to change the size of bar button items, please help me out.

Answer

Vince Yuan picture Vince Yuan · Sep 13, 2012

This is my solution. It works very well.

@interface UINavigationBar (CustomHeight)

@end

@implementation UINavigationBar (CustomHeight)

- (CGSize)sizeThatFits:(CGSize)size {
    // Change navigation bar height. The height must be even, otherwise there will be a white line above the navigation bar.
    CGSize newSize = CGSizeMake(self.frame.size.width, 40);
    return newSize;
}

-(void)layoutSubviews {
    [super layoutSubviews];

    // Make items on navigation bar vertically centered.
    int i = 0;
    for (UIView *view in self.subviews) {
        NSLog(@"%i. %@", i, [view description]);
        i++;
        if (i == 0)
            continue;
        float centerY = self.bounds.size.height / 2.0f;
        CGPoint center = view.center;
        center.y = centerY;
        view.center = center;
    }
}