iOS 更改狀態(tài)欄、導(dǎo)航欄顏色的幾種方法
ios上狀態(tài)欄 就是指的最上面的20像素高的部分
狀態(tài)欄分前后兩部分陕悬,要分清這兩個(gè)概念题暖,后面會(huì)用到:
前景部分:就是指的顯示電池、時(shí)間等部分;
背景部分:就是顯示黑色或者圖片的背景部分胧卤;
(一)設(shè)置statusBar的【前景部分】
簡(jiǎn)單來(lái)說(shuō)唯绍,就是設(shè)置顯示電池電量、時(shí)間枝誊、網(wǎng)絡(luò)部分標(biāo)示的顏色况芒, 這里只能設(shè)置兩種顏色:
默認(rèn)的黑色(UIStatusBarStyleDefault)
白色(UIStatusBarStyleLightContent)
可以設(shè)置的地方有兩個(gè):plist設(shè)置里面 和 程序代碼里
初始化設(shè)置:導(dǎo)航欄設(shè)置為不透明并給了"標(biāo)題"與狀態(tài)欄文字作對(duì)比
self.edgesForExtendedLayout =0;self.navigationItem.title =@"標(biāo)題";
只設(shè)置navigationBar不透明和寫(xiě)了一個(gè)標(biāo)題.png
改變狀態(tài)欄的方法
方法一:
1、plist
View controller-based status bar appearance 設(shè)置為 NO
設(shè)置為NO.png
2叶撒、代碼設(shè)置
[UIApplicationsharedApplication].statusBarStyle =UIStatusBarStyleLightContent;
效果如下:
狀態(tài)欄白色.png
方法二:
1绝骚、plist
View controller-based status bar appearance 設(shè)置為 YES 或者默認(rèn)(不設(shè)置)
注意:
如果View controller-based status bar appearance為YES。
則[UIApplication sharedApplication].statusBarStyle 無(wú)效痊乾。
2皮壁、代碼設(shè)置
self.navigationController.navigationBar.barStyle =UIBarStyleBlack;
狀態(tài)欄導(dǎo)航欄文字都白色,背景黑色.png
或者在控制器中重寫(xiě)?preferredStatusBarStyle方法,修改狀態(tài)欄顏色
- (UIStatusBarStyle)preferredStatusBarStyle {//? ? return UIStatusBarStyleLightContent;returnUIStatusBarStyleDefault;}
(二)設(shè)置statusBar的【背景部分】
背景部分,簡(jiǎn)單來(lái)說(shuō)哪审,就是背景色;改變方法有兩種:
1虑瀑、系統(tǒng)提供的方法
navigationBar的setBarTintColor接口湿滓,用此接口可改變statusBar的背景色
self.navigationController.navigationBar.barTintColor = [UIColorgreenColor];
純粹的背景色設(shè)置,默認(rèn)字體都是黑色.png
如果想將狀態(tài)欄和導(dǎo)航欄字體全變?yōu)榘咨?這樣就行
self.navigationController.navigationBar.barStyle =UIBarStyleBlack;
此行代碼能將狀態(tài)欄和導(dǎo)航欄字體顏色全體改變,只能是黑色或白色.png
如果只想改變導(dǎo)航欄的字體顏色,可以這樣
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColorredColor]}];
在默認(rèn)顯示的標(biāo)題中直接修改文件的大小和顏色也是可以的.png
還可以改變字體大小
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColorredColor],NSFontAttributeName:[UIFontsystemFontOfSize:25]}];
改變字體顏色大小.png
或者可以設(shè)置背景圖片
[self.navigationController.navigationBar setBackgroundImage:[UIImageimageNamed:@"image01"] forBarMetrics:UIBarMetricsDefault];
圖片背景.png
2、另辟蹊徑
創(chuàng)建一個(gè)UIView舌狗,
設(shè)置該UIView的frame.size 和statusBar大小一樣叽奥,
設(shè)置該UIView的frame.origin 為{0,-20},
設(shè)置該UIView的背景色為你希望的statusBar的顏色,
在navigationBar上addSubView該UIView即可痛侍。
原理:
狀態(tài)欄區(qū)域相對(duì)于navigationBar的區(qū)域?yàn)?/p>
{0,-20,self.view.bounds.size.width,20}
除了改變狀態(tài)欄的前景色(文字顏色,wifi顏色,時(shí)間顏色,電池顏色),就是改變背景色.由于狀態(tài)欄區(qū)域上的控件是隱藏的,所以只要在狀態(tài)欄區(qū)域被渲染了顏色,狀態(tài)欄的背景顏色就跟著一起改變,從而改變了狀態(tài)欄的背景顏色.
UIView*statusBarView = [[UIViewalloc]? initWithFrame:CGRectMake(0,-20,self.view.bounds.size.width,20)];statusBarView.backgroundColor = [UIColorgreenColor];[self.navigationController.navigationBar addSubview:statusBarView];
改變狀態(tài)欄的背景顏色.png
另外圖片透明處理
navigationBar為透明,注釋掉self.edgesForExtendedLayout = 0;
// self.edgesForExtendedLayout = 0;[self.navigationController.navigationBar setBackgroundImage:[UIImagenew] forBarMetrics:UIBarMetricsDefault]self.navigationController.navigationBar.shadowImage = [UIImagenew];
圖片透明設(shè)置.png