更改iPhone导航栏的高度

Hec*_*ret 5 iphone uinavigationbar

我的客户端无法读取iPhone的默认字体,尺寸太小.我有一个带导航栏的应用程序,我需要将其中的所有内容放大,例如,字体的大小.

IB似乎不允许这样......任何帮助?

非常感谢!

And*_*sov 13

更新:今天(2012年)有一个更大的自定义用户界面的趋势,所以我想说下面的答案太苛刻了.但是,仍然没有支持自定义高度的方法,但是你当然可以从UINavigationBar派生并覆盖一些大小调整方法.这可能不会让你被拒绝(虽然它仍然是一个灰色区域,只是苹果今天可能会忽视的东西).

获得所需大小后,可以使用iOS 5自定义API添加自定义背景图像(请参阅WWDC 2011会话114 - 自定义UIKit控件的外观).

2009年的原始答案:

这通常是不可能的.

更重要的是,我认为使导航栏更高是违反Apple人机界面指南的,因此您的应用程序可能会被App Store拒绝.在继续操作之前,请确保您的客户了解此风险.

(指出拒绝风险通常是说服客户做出无意义决策的好方法.)

  • @AvrahamShukron同意不同意见.我的意见在2009年可能是合理的,但肯定不是今天. (4认同)

Ske*_*ela 11

这里的许多答案都是不正确或不完整的,所以我想在这里添加我的答案,希望它可以启发一些.

首先,更改导航栏的高度没有任何问题.人们评论说不允许,或违反准则只是误解了这些指导方针.

自iOS 5以来,调整或更改UINavigationController中使用的默认导航栏的能力已成为SDK的一部分.

- (instancetype)initWithNavigationBarClass:(Class)navigationBarClass toolbarClass:(Class)toolbarClass NS_AVAILABLE_IOS(5_0);
Run Code Online (Sandbox Code Playgroud)

更改状态栏高度的最简单方法是在初始化导航控制器时使用此方法,并传入自定义UINavigationBar子类.

TestViewController *t = [[TestViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithNavigationBarClass:[MyNavigationBar class] toolbarClass:[UIToolbar class]];
[nav setViewControllers:@[t]];  
[self.window setRootViewController:nav];
[self.window makeKeyAndVisible];
Run Code Online (Sandbox Code Playgroud)

这种自定义UINavigationBar类的示例如下所示:

@interface MyNavigationBar : UINavigationBar
@end

@implementation MyNavigationBar

- (CGSize)sizeThatFits:(CGSize)size
{
        CGSize s = [super sizeThatFits:size];
        s.height = 90; // Or some other height
        return s;
}

@end
Run Code Online (Sandbox Code Playgroud)


Mar*_*eri 7

如果你决定只改变导航栏中的字体大小,你可以这样做(通常在你UIViewController的viewDidLoad方法中):

UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];

[titleLabel setBackgroundColor:[UIColor clearColor]];
// here's where you can customize the font size
[titleLabel setFont:[UIFont boldSystemFontOfSize:18.0]];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setText:self.title];
[titleLabel sizeToFit];
[titleLabel setCenter:[self.navigationItem.titleView center]];

[self.navigationItem setTitleView:titleLabel];

[titleLabel release];
Run Code Online (Sandbox Code Playgroud)


Avi*_*ron 5

通过子类化,您可以实现并仍然支持iOS 3+:

完整的例子:

#import <UIKit/UIKit.h>

@interface ASNavigationBar : UINavigationBar
@property (nonatomic , retain) UIImage *backgroundImage;
@end
Run Code Online (Sandbox Code Playgroud)

并实施:

#import "ASNavigationBar.h"

@implementation ASNavigationBar
@synthesize backgroundImage = _backgroundImage;

-(void) setBackgroundImage:(UIImage *)backgroundImage
{
    if (_backgroundImage != backgroundImage)
    {
        [_backgroundImage release];
        _backgroundImage = [backgroundImage retain];
        [self setNeedsDisplay];
    }
}

-(void) drawRect:(CGRect)rect
{
    // This is how the custom BG image is actually drawn
    [self.backgroundImage drawInRect:rect];
}

- (CGSize)sizeThatFits:(CGSize)size 
{
    // This is how you set the custom size of your UINavigationBar
    CGRect frame = [UIScreen mainScreen].applicationFrame;
    CGSize newSize = CGSizeMake(frame.size.width , self.backgroundImage.size.height);
    return newSize;
}
@end
Run Code Online (Sandbox Code Playgroud)

重要笔记:

  1. 如果背景图像具有透明区域,则必须将其barStyle属性设置为"半透明",否则透明区域将为黑色.
  2. 如果您的NavigationBar高于44磅,则必须考虑BarButtonItems的位置可能不正确.它们都将固定在酒吧的底部.您可以通过覆盖layoutSubviews并更改其origin.y值来解决此问题.