1. 程式人生 > >ios 狀態列statusBar的背景顏色

ios 狀態列statusBar的背景顏色

ios 狀態列statusBar的背景顏色

一、無導航條的情況:

系統預設狀態列的字型顏色為黑色,即UIStatusBarStyle=UIStatusBarStyleDefault,同時背景顏色和self.view.backgroundColor顏色一致,如下圖所示:


  14F49066-52A9-4892-AF66-D2F9ED0D9001.png

假如我想讓狀態列顏色設定成紅色,字型仍為黑色,可以在需要顯示的那一頁進行如下設定:(最好寫在viewWillAppear裡面)

//設定狀態列顏色
- (void)setStatusBarBackgroundColor:(UIColor *)color {
    
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"]; if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) { statusBar.backgroundColor = color; } } - (void)viewDidLoad { [super viewDidLoad]; [self setStatusBarBackgroundColor:[UIColor redColor]]; self.view.backgroundColor = [UIColor yellowColor]; } 

效果如下:

  50B2AB94-49CE-49AB-87A0-E6326C1CE00B.png

假如此時我想讓狀態列文字顏色變成白色,可以這樣操作:
在上面程式碼的基礎上再新增下面一段程式碼:

- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

效果如下:


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

問題來了,當你在這一頁點選按鈕進入下一頁後,狀態列背景顏色不變,還是紅色,而字型顏色卻變成黑色了,比較鬧心!可以通過下面的方法隨心所欲的在任意一頁修改狀態列的字型顏色(字型顏色只有白色和黑色)和背景顏色(直接複製到專案中即可

//設定字型顏色
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;//白色 } //設定狀態列顏色 - (void)setStatusBarBackgroundColor:(UIColor *)color { UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"]; if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) { statusBar.backgroundColor = color; } } //!!!重點在viewWillAppear方法裡呼叫下面兩個方法 -(void)viewWillAppear:(BOOL)animated{ [self preferredStatusBarStyle]; [self setStatusBarBackgroundColor:[UIColor redColor]]; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor yellowColor]; } 

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


  9B2DA0CD-C106-4255-BA02-F34F55D59E99.png   50007497-1481-4F1E-9852-81BFD9CA7B9C.png

二、有導航條的情況

當我在上面的基礎上添加了導航條後,會發現字型顏色由之前的白色變成黑色了,背景顏色倒沒有發生變化


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

不用擔心,可以通過下面的方法完美解決:

//設定狀態列顏色
- (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; } --->!!!同時別忘了在info plist裡面將View controller-based status bar appearance設定成NO,(預設是YES) 
現在基本的裝置都適配ios7以上裝置,預設的狀態列字型顏色是黑色
[UIApplicationsharedApplication].statusBarStyle=UIStatusBarStyleDefault;
現在基本的裝置都適配ios7以上裝置,預設的狀態列字型顏色是黑色
[UIApplicationsharedApplication].statusBarStyle=UIStatusBarStyleLightContent;
  EC111815-4423-479F-BCBD-ADAE52F34E7C.png

 

  54BC597D-F8B3-4EB5-BD0C-394AC0439073.png
Demo下載地址: https://github.com/zhuchenglong/StatusBarDemo

 

以下是我在實際專案中使用的:

//設定狀態列顏色
- (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]; //Y起點在導航條下面 self.edgesForExtendedLayout = UIRectEdgeNone; //設定navigationItem返回的文字 UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil]; self.navigationItem.backBarButtonItem = item; } -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; //設定導航條透明度 self.navigationController.navigationBar.translucent = NO;//不透明 [[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:1]; //圖示顏色為黑色 [self.navigationController.navigationBar setTintColor:[UIColor blackColor]]; //導航欄背景顏色 [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]]; //導航條下面的黑線 self.navigationController.navigationBar.clipsToBounds = NO; //重新整理狀態列背景顏色 // [self setNeedsStatusBarAppearanceUpdate]; //設定狀態列顏色 [self setStatusBarBackgroundColor:[UIColor blackColor]]; } //一定要在viewWillDisappear裡面寫,如果寫在viewDidDisappear裡面會出問題!!!! - (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; //為了不影響其他頁面在viewDidDisappear做以下設定 self.navigationController.navigationBar.translucent = YES;//透明 [self setStatusBarBackgroundColor:[UIColor clearColor]]; } 

效果:


  81331546-9FF9-4314-96D7-29798D9034B0.png   13D7939F-E318-471C-9EBC-E1E92E619685.png