【问题标题】:How do I change the colour of the status bar when the navigation bar is hidden in iOS 7?iOS 7隐藏导航栏时如何更改状态栏的颜色?
【发布时间】:2014-02-02 04:13:17
【问题描述】:

我知道如何通过这样做来更改导航栏(和状态栏)的颜色:

self.navigationController.navigationBar.barTintColor = [UIColor redColor];

但是当我隐藏导航栏时,状态栏颜色恢复为透明颜色。

即使隐藏导航栏,如何保持状态栏颜色与 barTintColor 相同?

【问题讨论】:

  • 在其后面添加一个具有相同色调的子视图
  • 可能有助于了解状态栏始终是透明的。在它后面放一个UIView 与在它后面放一个UINavigationBar 没有什么不同。
  • @nhgrif 是的,这是真的,但状态栏是 320 x 20,与导航栏的尺寸不同。

标签: ios ios7 uikit ios7-statusbar


【解决方案1】:

这对我有用(在 Swift 中):

let statusBarBGView = UIView(frame: UIApplication.sharedApplication().statusBarFrame)
statusBarBGView.backgroundColor = .whiteColor() //color of your choice
view.addSubview(statusBarBGView)

我的问题是我将导航栏设置为隐藏,这使得状态栏的背景变得清晰。这导致我的 tableView 单元格在滚动时显示在 statusBar 后面。

【讨论】:

    【解决方案2】:

    您只需将UIView 添加到具有正确状态栏测量值的当前视图,然后更改颜色。

    这里有一些示例代码。 先获取状态栏的frame:

     //The statusBarFrame returns the frame in screen coordinates. I believe the correct way to get what this corresponds to in view coordinates is to do the following:
    - (CGRect)statusBarFrameViewRect:(UIView*)view 
    {
        CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
        CGRect statusBarWindowRect = [view.window convertRect:statusBarFrame fromWindow: nil];
        CGRect statusBarViewRect = [view convertRect:statusBarWindowRect fromView: nil];
        return statusBarViewRect;
    }
    //source: http://stackoverflow.com/questions/3888517/get-iphone-status-bar-height
    

    然后在 viewDidload 方法中或在您隐藏导航栏时使用以下代码创建您的视图:

    UIView *statusBarUnderLay = [[UIView alloc] initWithFrame:[self statusBarFrameViewRect:self.view]];
    [statusBarUnderLay setBackgroundColor:[UIColor yellowColor]];
    [self.view addSubview:statusBarUnderLay];
    

    然后瞧

    【讨论】:

    • 你们想看这个的快速版本吗?还是代码足够简单可以转换?
    【解决方案3】:

    在状态栏下方添加UIView,并将其backgroundColor属性设置为导航栏barTintColor

    【讨论】:

      【解决方案4】:

      我的解决方案是简单地设置视图控制器视图的背景颜色。例如

      [self.view setBackgroundColor:[UIColor blackColor]];

      当操作系统选择不显示状态栏时,需要处理添加视图。

      【讨论】:

        【解决方案5】:

        我找到了一种使用 InterfaceBuilder 解决问题的简单方法。 我的问题是这样的,隐藏导航栏后有一个恼人的白色间隙。

        [

        然后我取消选中这两个选项

        然后就可以了

        【讨论】:

          【解决方案6】:

          self.navigationController?.navigationBarHidden = true之后设置self.tableView.contentInset = UIEdgeInsetsZero

          【讨论】:

            【解决方案7】:

            这解决了我的问题:

            - (UIStatusBarStyle)preferredStatusBarStyle
            {
                return UIStatusBarStyleLightContent;
            }
            

            【讨论】:

              猜你喜欢
              • 2015-05-07
              • 1970-01-01
              • 1970-01-01
              • 2015-08-03
              • 1970-01-01
              • 2013-09-26
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多