您的位置:首页 > 产品设计 > UI/UE

iphone中UINavigationBar(导航条视图)的介绍

2015-09-12 12:25 633 查看
多视图应用程序中,我们常常使用到自定义UINavigationBar来完成导航条的设置。

1.创建一个导航条

UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];


2.有了导航条以后,必须在导航条上设置一个item集合,用来放置中间的标题,和左右的按钮,因为上面空间有限,只有左右两个按钮。
//创建一个导航条集合
UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:nil];
3.设置导航条样式(使用系统自带样式)

[navBar setBarStyle:UIBarStyleDefault];


分别有如下几种样式:

typedef NS_ENUM(NSInteger, UIBarStyle) {
UIBarStyleDefault          = 0,
UIBarStyleBlack            = 1,
UIBarStyleBlackOpaque      = 1, // Deprecated. Use UIBarStyleBlack
UIBarStyleBlackTranslucent = 2, // Deprecated. Use UIBarStyleBlack and set the translucent property to YES
};


从字面我们就能了解这4种样式的大概意思:
分别为:
UIBarStyleDefault:默认样式
UIBarStyleBlack:黑色
UIBarStyleBlackOpaque:黑色不透明
UIBarStyleBlackTranslucent:黑色透明

注意:我们发现,在后面两个标记为Deprecated,我们知道使用后面两种将不被提倡。
从枚举中,我们也可以看出:UIBarStyleBlack=1和UIBarStyleBlackOpaque=1表示为一样的。
后来,发现增加了一个方法:[navBar setTranslucent:YES];用来指示是否透明。

所以,我们使用UIBarStyleDefault和UIBarStyleBlack来定义UINavigationBar样式,并且用setTranslucent:方法来设置透明与否。

3.自定义导航条颜色

如果,仅仅使用这4种(2种样式*是否透明),难免太逊了,必须能自定义UINavigationBar样式啊。

[navBar setBackgroundImage:[UIImage imageNamed: @"图片名称"] forBarMetrics:UIBarMetricsDefault];

setBackgroundImage方法的第二个参数,需要解释一下:

UIBarMetricsDefault:用竖着(拿手机)时UINavigationBar的标准的尺寸来显示UINavigationBar
UIBarMetricsLandscapePhone:用横着时UINavigationBar的标准尺寸来显示UINavigationBar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: