在Cocoa Touch中更改其中的NavigationBar和UIBarButtonItem元素的高度

mga*_*mer 19 cocoa-touch uinavigationbar uinavigationcontroller uibarbuttonitem

我认为它并不严格符合Apple指南,但我想它必定是可能的.我想更改UINavigationController中导航栏的高度以及该栏内UIBarButtonItem元素的高度.

使用这个问题的技巧我设法改变导航栏的高度,但我看不到调整条形按钮项高度的方法.

如果有人知道如何更改条形按钮项的大小,请帮助我.

Vin*_*uan 18

这是我的解决方案.它工作得很好.

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我从不0,为什么要检查? (2认同)

Pfi*_*itz 16

也许这个关于自定义导航栏的教程将帮助您:重新创建iBooks木材主题导航栏

如果BarButtonItem使用a 创建a ,UIImageView则可以更改自定义UIImageView的framesize/boundsize

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];
Run Code Online (Sandbox Code Playgroud)

因此,根据您的需要,您将为-initWithFrame方法提供适当的值.