ios上狀態欄 就是指的最上面的20像素高的部分
狀態欄分前后兩部分,要分清這兩個概念,后面會用到:
前景部分:就是指的顯示電池、時間等部分;
背景部分:就是顯示黑色或者圖片的背景部分;
(一)設置statusBar的【前景部分】
簡單來說,就是設置顯示電池電量、時間、網絡部分標示的顏色, 這里只能設置兩種顏色:
默認的黑色(UIStatusBarStyleDefault)
白色(UIStatusBarStyleLightContent)
可以設置的地方有兩個:plist設置里面 和 程序代碼里
初始化設置:導航欄設置為不透明并給了"標題"與狀態欄文字作對比
改變狀態欄的方法
方法一:
1、plist
View controller-based status bar appearance 設置為 NO
2、代碼設置
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
//相對于上面的接口,這個接口可以動畫的改變statusBar的前景色
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
方法二:
1、plist
View controller-based status bar appearance 設置為 YES 或者默認(不設置)
注意:
如果View controller-based status bar appearance為YES。
則[UIApplication sharedApplication].statusBarStyle 無效。
2、代碼設置
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
或者在控制器中重寫 preferredStatusBarStyle方法,修改狀態欄顏色
?其他方法
不僅如此,ios還很貼心的在UIViewController也增加了幾個接口,
目的是讓狀態欄根據當前顯示的UIViewController來定制statusBar的前景部分。
- (UIStatusBarStyle)preferredStatusBarStyle;
- (UIViewController *)childViewControllerForStatusBarStyle;
- (void)setNeedsStatusBarAppearanceUpdate
- (UIStatusBarStyle)preferredStatusBarStyle:
在你自己的UIViewController里重寫此方法,返回你需要的值(UIStatusBarStyleDefault 或者 UIStatusBarStyleLightContent);
注意:
這里如果你只是簡單的return一個固定的值,那么該UIViewController顯示的時候,程序就會馬上調用該方法,來改變statusBar的前景部分;
如果在該UIViewController已經在顯示在當前,你可能還要在當前頁面不時的更改statusBar的前景色,那么,你首先需要調用下面的setNeedsStatusBarAppearanceUpdate方法(這個方法會通知系統去調用當前UIViewController的preferredStatusBarStyle方法), 這個和UIView的setNeedsDisplay原理差不多(調用UIView對象的setNeedsDisplay方法后,系統會在下次頁面刷新時,調用重繪該view,系統最快能1秒刷新60次頁面,具體要看程序設置)。
- (UIViewController *)childViewControllerForStatusBarStyle:
這個接口也很重要,默認返回值為nil。當我們調用setNeedsStatusBarAppearanceUpdate時,系統會調用application.window的rootViewController的preferredStatusBarStyle方法,我們的程序里一般都是用UINavigationController做root,如果是這種情況,那我們自己的UIViewController里的preferredStatusBarStyle根本不會被調用;
這種情況下childViewControllerForStatusBarStyle就派上用場了,
我們要子類化一個UINavigationController,在這個子類里面重寫childViewControllerForStatusBarStyle方法,如下:
- (UIViewController *)childViewControllerForStatusBarStyle{
return self.topViewController;
}
上面代碼的意思就是說,不要調用我自己(就是UINavigationController)的preferredStatusBarStyle方法,而是去調用navigationController.topViewController的preferredStatusBarStyle方法,這樣寫的話,就能保證當前顯示的UIViewController的preferredStatusBarStyle方法能影響statusBar的前景部分。
另外,有時我們的當前顯示的UIViewController可能有多個childViewController,重寫當前UIViewController的childViewControllerForStatusBarStyle方法,讓childViewController的preferredStatusBarStyle生效(當前UIViewController的preferredStatusBarStyle就不會被調用了)。
簡單來說,只要UIViewController重寫的的childViewControllerForStatusBarStyle方法返回值不是nil,那么,UIViewController的preferredStatusBarStyle方法就不會被系統調用,系統會調用childViewControllerForStatusBarStyle方法返回的UIViewController的preferredStatusBarStyle方法。
- (void)setNeedsStatusBarAppearanceUpdate:
讓系統去調用application.window的rootViewController的preferredStatusBarStyle方法,如果rootViewController的childViewControllerForStatusBarStyle返回值不為nil,則參考上面的講解。
(二)設置statusBar的【背景部分】
背景部分,簡單來說,就是背景色;改變方法有兩種:
1、系統提供的方法
navigationBar的setBarTintColor接口,用此接口可改變statusBar的背景色
self.navigationController.navigationBar.barTintColor = [UIColor greenColor];
如果想將狀態欄和導航欄字體全變為白色,這樣就行
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
如果只想改變導航欄的字體顏色,可以這樣
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];
還可以改變字體大小
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:25]}];
或者可以設置背景圖片
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"image01"] forBarMetrics:UIBarMetricsDefault];
2、另辟蹊徑
創建一個UIView,
設置該UIView的frame.size 和statusBar大小一樣,
設置該UIView的frame.origin 為{0,-20},
設置該UIView的背景色為你希望的statusBar的顏色,
在navigationBar上addSubView該UIView即可。
原理:
狀態欄區域相對于navigationBar的區域 ?{0,-20,self.view.bounds.size.width,20}
除了改變狀態欄的前景色(文字顏色,wifi顏色,時間顏色,電池顏色),就是改變背景色.由于狀態欄區域上的控件是隱藏的,所以只要在狀態欄區域被渲染了顏色,狀態欄的背景顏色就跟著一起改變,從而改變了狀態欄的背景顏色.
UIView *statusBarView = [[UIView alloc]? initWithFrame:CGRectMake(0, -20,? ? self.view.bounds.size.width, 20)];
statusBarView.backgroundColor = [UIColor greenColor];
[self.navigationController.navigationBar addSubview:statusBarView];
另外圖片透明處理
navigationBar為透明,注釋掉self.edgesForExtendedLayout = 0;
// self.edgesForExtendedLayout = 0;
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]
self.navigationController.navigationBar.shadowImage = [UIImage new];