在iOS 7中隐藏导航栏时,如何更改状态栏的颜色?

问题描述 投票:5回答:7

我知道如何通过执行以下操作来更改导航栏(和状态栏)的颜色:

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

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

即使导航栏被隐藏,如何使状态栏颜色与barTintColor保持一致?

ios ios7 uikit ios7-statusbar
7个回答
2
投票

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


6
投票

您只需使用正确的statusBar测量值将UIView添加到当前视图,然后更改颜色。

这是一些示例代码。首先获取状态栏的框架:

 //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 yellow]];
[self.view addSubview:statusBarUnderLay];


2
投票

这对我来说很有用(在Swift中):

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

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


2
投票

我的解决方案很简单,设置viewcontrollers视图的背景颜色。例如

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

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


1
投票

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

[

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

options

那它有效

after## Heading ##


0
投票

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


0
投票

这解决了我的问题:

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
© www.soinside.com 2019 - 2024. All rights reserved.