UINavigationBar-使用总结

来源:互联网 发布:java socket服务端程序 编辑:程序博客网 时间:2024/05/16 05:10

转载自:http://blog.sina.com.cn/s/blog_7b9d64af01019zsi.html

多视图应用程序中,我们常常使用到自定义UINavigationBar来完成导航条的设置。

1.获取导航条

UINavigationBar *navBar = self.navigationController.navigationBar;


2.设置导航条样式(使用系统自带样式)

[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:默认样式
UINavigationBar-使用总结

UIBarStyleBlack:黑色
UINavigationBar-使用总结

UIBarStyleBlackOpaque:黑色不透明
UINavigationBar-使用总结

UIBarStyleBlackTranslucent:黑色透明
UINavigationBar-使用总结

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

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

3.自定义导航条颜色

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

if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]){

        // UIBarMetricsLandscapePhone

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

    }


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

UIBarMetricsDefault:用竖着(拿手机)时UINavigationBar的标准的尺寸来显示UINavigationBar

UIBarMetricsLandscapePhone:用横着UINavigationBar标准尺寸来显示UINavigationBar


希望对您有所帮助!

0 0
原创粉丝点击