ios 狀態(tài)欄statusBar的背景顏色

ios 狀態(tài)欄statusBar的背景顏色

一、無導(dǎo)航條的情況:

系統(tǒng)默認(rèn)狀態(tài)欄的字體顏色為黑色,即UIStatusBarStyle=UIStatusBarStyleDefault,同時(shí)背景顏色和self.view.backgroundColor顏色一致,如下圖所示:

假如我想讓狀態(tài)欄顏色設(shè)置成紅色,字體仍為黑色,可以在需要顯示的那一頁進(jìn)行如下設(shè)置:(最好寫在viewWillAppear里面)

//設(shè)置狀態(tài)欄顏色- (void)setStatusBarBackgroundColor:(UIColor*)color{

UIView*statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];

if([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {

? ? ? ? statusBar.backgroundColor = color;? ?

}}

- (void)viewDidLoad {? ?

[superviewDidLoad];? ? ?

[self setStatusBarBackgroundColor:[UIColor redColor]];

self.view.backgroundColor = [UIColor yellowColor];

}

效果如下:

假如此時(shí)我想讓狀態(tài)欄文字顏色變成白色,可以這樣操作:

在上面代碼的基礎(chǔ)上再添加下面一段代碼:

- (UIStatusBarStyle)preferredStatusBarStyle{

return UIStatusBarStyleLightContent;

}

效果如下:

ED915548-3B47-45EE-AA27-7D068881A946.png

問題來了,當(dāng)你在這一頁點(diǎn)擊按鈕進(jìn)入下一頁后,狀態(tài)欄背景顏色不變,還是紅色,而字體顏色卻變成黑色了,比較鬧心!可以通過下面的方法隨心所欲的在任意一頁修改狀態(tài)欄的字體顏色(字體顏色只有白色和黑色)和背景顏色(直接復(fù)制到項(xiàng)目中即可

//設(shè)置字體顏色- (UIStatusBarStyle)preferredStatusBarStyle{returnUIStatusBarStyleLightContent;//白色}//設(shè)置狀態(tài)欄顏色- (void)setStatusBarBackgroundColor:(UIColor*)color{

UIView*statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];

if([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {? ? ? ?

statusBar.backgroundColor = color;? ?

}

}

//?。?!重點(diǎn)在viewWillAppear方法里調(diào)用下面兩個(gè)方法

-(void)viewWillAppear:(BOOL)animated{? ?

[self preferredStatusBarStyle];? ?

[self setStatusBarBackgroundColor:[UIColor redColor]];

}

- (void)viewDidLoad {?

? [superviewDidLoad];

self.view.backgroundColor = [UIColor yellowColor];?

}

下面的效果是第一頁要紅底白字,第二頁要綠底黑字,返回后也是正常顯示


二、有導(dǎo)航條的情況

當(dāng)我在上面的基礎(chǔ)上添加了導(dǎo)航條后,會(huì)發(fā)現(xiàn)字體顏色由之前的白色變成黑色了,背景顏色倒沒有發(fā)生變化

46CA6B3F-45D6-4628-9D34-0967320616D0.png

不用擔(dān)心,可以通過下面的方法完美解決:

//設(shè)置狀態(tài)欄顏色

- (void)setStatusBarBackgroundColor:(UIColor*)color {

UIView*statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];if([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {? ? ? ?

statusBar.backgroundColor = color;?

? }

}

-(void)viewWillAppear:(BOOL)animated{? ?

[self setStatusBarBackgroundColor:[UIColor redColor]];? ? [UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleLightContent;

}--->

?。?!同時(shí)別忘了在info plist里面將View controller-based status bar appearance設(shè)置成NO,(默認(rèn)是YES)

現(xiàn)在基本的設(shè)備都適配ios7以上設(shè)備,默認(rèn)的狀態(tài)欄字體顏色是黑色[UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleDefault;現(xiàn)在基本的設(shè)備都適配ios7以上設(shè)備,默認(rèn)的狀態(tài)欄字體顏色是黑色[UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleLightContent;



Demo下載地址:https://github.com/zhuchenglong/StatusBarDemo

以下是我在實(shí)際項(xiàng)目中使用的:

//設(shè)置狀態(tài)欄顏色

- (void)setStatusBarBackgroundColor:(UIColor *)color {

UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"]; NSLog(@"statusBar.backgroundColor--->%@",statusBar.backgroundColor);

if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {

statusBar.backgroundColor = color;

}

}

- (UIStatusBarStyle)preferredStatusBarStyle{?

return UIStatusBarStyleLightContent;//白色

}

- (void)viewDidLoad {

?[super viewDidLoad];?

self.edgesForExtendedLayout = UIRectEdgeNone;//Y起點(diǎn)在導(dǎo)航條下面?

//設(shè)置navigationItem返回的文字

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil]; self.navigationItem.backBarButtonItem = item;

}

-(void)viewWillAppear:(BOOL)animated{

[super viewWillAppear:animated];

//設(shè)置導(dǎo)航條透明度

self.navigationController.navigationBar.translucent = NO;//不透明

[[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:1];

//圖標(biāo)顏色為黑色?

[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];?

//導(dǎo)航欄背景顏色

[self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];

?//導(dǎo)航條下面的黑線?

self.navigationController.navigationBar.clipsToBounds = NO;

//刷新狀態(tài)欄背景顏色?

// [self setNeedsStatusBarAppearanceUpdate];?

//設(shè)置狀態(tài)欄顏色

?[self setStatusBarBackgroundColor:[UIColor blackColor]];

}

//一定要在viewWillDisappear里面寫,如果寫在viewDidDisappear里面會(huì)出問題?。。?!

?- (void)viewWillDisappear:(BOOL)animated{

[super viewWillDisappear:animated];?

//為了不影響其他頁面在viewDidDisappear做以下設(shè)置

self.navigationController.navigationBar.translucent = YES;

?//透明

?[self setStatusBarBackgroundColor:[UIColor clearColor]];

?}

效果






最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容