【问题标题】:How to change status bar color in iOS7如何在 iOS7 中更改状态栏颜色
【发布时间】:2015-06-11 03:49:33
【问题描述】:

我创建了一个自定义导航栏,我需要将状态栏的颜色更改为与导航栏的背景颜色相同:

- (void)drawRect:(CGRect)rect {
    CGFloat logoWidth = CGRectGetWidth([self frame]) / 4;
    CGFloat logoHeight =CGRectGetHeight([self frame]) / 2;
    [kAPPLogo drawInRect:CGRectMake((CGRectGetWidth([self frame]) - logoWidth) / 2, (CGRectGetHeight([self frame]) - logoHeight) / 2, logoWidth, logoHeight)];

    self.barTintColor = kNavicationBarColor;
    self.backgroundColor = kNavicationBarColor;
}

我试过这个:

self.barTintColor = kNavicationBarColor

但它不起作用。有人可以帮忙吗?

【问题讨论】:

    标签: ios statusbar navigationbar


    【解决方案1】:

    没有直接改变状态栏颜色的方法。我们可以使用setStatusBarStyle 属性选择状态栏样式,然后在三种可用样式中进行选择:

    UIStatusBarStyleDefault
    UIStatusBarStyleBlackTranslucent
    UIStatusBarStyleBlackOpaque
    

    但是,如果您更改 UIWindow 对象的背景颜色并将状态栏样式设置为 UIStatusBarStyleBlackTranslucent,这会将状态栏的颜色设置为与窗口的背景颜色相同。

    将以下代码添加到applicationDidFinishLaunchingWithOptions 中的AppDeligate.m 文件中:

    self.window.backgroundColor = kNavicationBarColor;
    [application setStatusBarStyle:UIStatusBarStyleBlackTranslucent];
    

    【讨论】:

      【解决方案2】:

      状态栏背景颜色是透明的,所以不需要改变它,它的内容颜色只有白色和黑色两种颜色。要更改它的内容颜色,请覆盖方法preferredStatusBarStyle,如下所示

      - (UIStatusBarStyle)preferredStatusBarStyle
      {
          return UIStatusBarStyleLightContent;
      }
      

      viewDidLoad调用这个方法

      即使你想改变状态栏的背景颜色,你也可以通过以下代码来实现:

      UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
      statusBar.backgroundColor = [UIColor blueColor];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-05
        • 1970-01-01
        • 1970-01-01
        • 2023-03-05
        • 2014-03-16
        相关资源
        最近更新 更多