多視圖應用程序中氢哮,我們常常使用到自定義UINavigationBar來完成導航條的設置。
1.獲取導航條
UINavigationBar*navBar =self.navigationController.navigationBar;
2.設置導航條樣式(使用系統(tǒng)自帶樣式)
[navBarsetBarStyle:UIBarStyleDefault];
分別有如下幾種樣式:
typedefNS_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:黑色透明
注意:
我們發(fā)現(xiàn)千扔,在后面兩個標記為Deprecated憎妙,我們知道使用后面兩種將不被提倡。
從枚舉中曲楚,我們也可以看出:UIBarStyleBlack=1和UIBarStyleBlackOpaque=1表示為一樣的厘唾。
后來,發(fā)現(xiàn)增加了一個方法:[navBarsetTranslucent:YES];用來指示是否透明龙誊。
所以抚垃,我們使用UIBarStyleDefault和UIBarStyleBlack來定義UINavigationBar樣式,并且用setTranslucent:方法來設置透明與否趟大。
3.自定義導航條顏色
如果鹤树,僅僅使用這4種(2種樣式*是否透明),難免太遜了逊朽,必須能自定義UINavigationBar樣式啊魂迄。
if([navBarrespondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]){
// UIBarMetricsLandscapePhone
[navBarsetBackgroundImage:[UIImageimageNamed:@"圖片名稱"]forBarMetrics:UIBarMetricsDefault];
}
setBackgroundImage方法的第二個參數(shù),需要解釋一下:
UIBarMetricsDefault:用豎著(拿手機)時UINavigationBar的標準的尺寸來顯示UINavigationBar
UIBarMetricsLandscapePhone:用橫著時UINavigationBar的標準尺寸來顯示UINavigationBar
希望對您有所幫助惋耙!