簡體   English   中英

為什么狀態欄和導航欄背景顏色在iOS中不同

[英]why Status bar and Navigation bar background colors are different in ios

我想更改狀態欄的背景色,並且該背景色應與導航欄的背景色相同,即深灰色。 我的問題是使用下面的代碼為狀態欄和導航欄賦予相同的顏色后,我得到了不同的顏色。 我已經將UIViewControllerBasedStatusBarAppearance設置為NO。 請建議我我做錯了什么,下面附上圖片供您參考。

在此處輸入圖片說明

這是我的應用程序委托代碼-didFinishLaunchingWithOptions:

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    //For changing status bar background color

    self.window.backgroundColor = [UIColor darkGrayColor];

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];

    //For changing navigation bar background color

    [[UINavigationBar appearance] setBackgroundColor:[UIColor darkGrayColor]];
    [[UINavigationBar appearance] setTintColor:[UIColor darkGrayColor]];

在此處輸入圖片說明

它們具有不同顏色的原因是因為UIStatusBarStyleLightContent[UIColor darkGrayColor]顏色[UIColor darkGrayColor]


如果可以根據狀態欄的外觀來調整導航欄顏色,則可以設置狀態欄顏色:

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

找出狀態欄的RGB值。 (例如,我使用DigitalColor Meter,它表示具有R:28,G:28,B:28)。

然后,您可以執行以下操作:

[[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithRed:28/255.0 green:28/255.0 blue:28/255.0 alpha:1.0f]];

您可能需要將透明度設置為NO

如果僅在某些屏幕上需要它,可以將其添加到viewDidLoad方法:

[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:28/255.0 green:28/255.0 blue:28/255.0 alpha:1.0f]];

首先在您的VC的viewDidLoad方法中更改statusBar樣式

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

然后在VC的viewDidLoad方法中將BarTintColor更改為所需的顏色

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

在您的AppDelegate的'didFinishLaunchingWithOptions:'中添加以下內容,它應該可以正常工作。

[[UINavigationBar appearance] setBarTintColor:[UIColor darkGrayColor]];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM