Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 270
mgamer Avatar asked Nov 27 '11 13:11

mgamer


3 Answers

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;
    }
}
like image 72
Vince Yuan Avatar answered Oct 16 '22 11:10

Vince Yuan


Maybe this tutorial about a customized navbar will help you: Recreating the iBooks wood themed navigation bar

If you create a BarButtonItem with a UIImageView you can maybe change the framesize/boundsize of the custom UIImageView

UIImageView* imageView = [[[UIImageView alloc] initWithFrame:navigationController.navigationBar.frame] autorelease];
imageView.contentMode = UIViewContentModeLeft;
imageView.image = [UIImage imageNamed:@"NavBar-iPhone.png"];
[navigationController.navigationBar insertSubview:imageView atIndex:0];

So for your need you would give the -initWithFrame method appropriate values.

like image 43
Pfitz Avatar answered Oct 16 '22 11:10

Pfitz


static CGFloat const CustomNavigationBarHeight = 74;


@implementation WTNavigationBar



- (CGSize)sizeThatFits:(CGSize)size{
    size.width = 1024;
    size.height = CustomNavigationBarHeight;
    return size;
}

-(void)layoutSubviews {
    [super layoutSubviews];
    for (UIView *view in self.subviews) {
        SFLog(@"view.class=%@",[view class]);
        if ([view isKindOfClass:NSClassFromString(@"UINavigationItemButtonView")]) {
            float centerY = self.bounds.size.height / 2.0f;
            CGPoint center = view.center;
            center.y = centerY;
            view.center = center;
        }
    }
}


@end

in my iPad app,which has a fixed landscape orientation,I found I have to hardcode the size's width

like image 4
Jagie Avatar answered Oct 16 '22 13:10

Jagie