一.修改狀態(tài)欄文字顏色
這里修改文字顏色分兩種情況
(1)導(dǎo)航欄是隱藏狀態(tài)
如果導(dǎo)航欄為隱藏狀態(tài) 可以直接在控制器中重寫(xiě)如下方法
- (UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
(2)導(dǎo)航欄不是隱藏狀態(tài)
如果導(dǎo)航欄不是隱藏狀態(tài) 會(huì)發(fā)現(xiàn)方法(1)沒(méi)有作用了
這時(shí)要采用第二種方法, 一共有兩個(gè)步驟
1.在info.plist
添加字段 View controller-based status bar appearance
并設(shè)置為NO
(如果沒(méi)有此字段 請(qǐng)?zhí)砑?
2.在代碼中寫(xiě)上如下代碼
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
如果你做了上面兩個(gè)步驟且操作沒(méi)有錯(cuò)誤的話 就會(huì)發(fā)現(xiàn)狀態(tài)欄文字變?yōu)榱税咨?/p>
二.修改狀態(tài)欄背景顏色
iOS13此方法會(huì)導(dǎo)致崩潰
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor = [UIColor cyanColor];
}
取代方法
取代方法是直接在self.view上加一個(gè)view或layer
CGFloat stateBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
UIView *blockView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, stateBarHeight)];
blockView.backgroundColor = [UIColor redColor];
[self.view addSubview:blockView];