多视图应用程序中,我们常常使用到自定义UINavigationBar来完成导航条的设置。
1.获取导航条
UINavigationBar *navBar =self.navigationController.navigationBar;
//创建导航栏对象
2.设置导航条样式(使用系统自带样式)
[navBar setBarStyle:UIBarStyleDefault];
分别有如下几种样式:
typedef NS_ENUM(NSInteger, UIBarStyle) {
UIBarStyleDefault =0,
UIBarStyleBlack = 1,
UIBarStyleBlackOpaque = 1,// Deprecated. UseUIBarStyleBlack
UIBarStyleBlackTranslucent = 2, //Deprecated. Use UIBarStyleBlack and set the translucent property toYES
};
我们会发现有两个枚举的值都是 1 所以 可以肯定的是 这两种情况的 效果 是有一样的 具体效果 可以自己实现。
3.自定义导航条颜色
如果,仅仅使用这4种(2种样式*是否透明),难免太逊了,必须能自定义
UINavigationBar样式啊。
if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]){
//UIBarMetricsLandscapePhone
[navBarsetBackgroundImage:[UIImageimageNamed:@"图片名称"] forBarMetrics:UIBarMetricsDefault];
}
setBackgroundImage方法的第二个参数,需要解释一下:
UIBarMetricsDefault:用竖着(拿手机)时
UINavigationBar的标准的尺寸来显示
UINavigationBar
UIBarMetricsLandscapePhone:用横着时UINavigationBar的标准尺寸来显示UINavigationBar