首页 文章

为什么状态栏和导航栏背景颜色在ios中不同

提问于
浏览
1

我想更改状态栏的背景颜色,背景颜色应与导航栏背景颜色相同,即深灰色 . 我的问题是在使用下面的代码为状态栏和导航栏提供相同的颜色后,我得到了不同的颜色 . 我已经将UIViewControllerBasedStatusBarAppearance设置为NO . 请建议我在哪里做错了,我附上图片供您参考 .

enter image description here

这是我的App委托代码 - 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]];

enter image description here

3 回答

  • 0

    他们有不同颜色的原因是因为 UIStatusBarStyleLightContent[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]];
    
  • 0

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

    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    

    然后在VC的viewDidLoad方法中将 BarTintColor 更改为所需的颜色

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

    在你的 AppDelegate 's ' didFinishLaunchingWithOptions:'添加以下内容,它应该有效 .

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

相关问题