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

当导航栏隐藏在iOS 7中时,如何更改状态栏的颜色?,ios,ios7,uikit,ios7-statusbar,Ios,Ios7,Uikit,Ios7 Statusbar,我知道如何通过以下操作更改导航栏(和状态栏)的颜色: self.navigationController.navigationBar.barTintColor = [UIColor redColor]; 但当我隐藏导航栏时,状态栏的颜色将恢复为透明颜色 即使导航栏处于隐藏状态,如何保持状态栏颜色与BartinColor相同?在状态栏下添加一个UIView,并将其backgroundColor属性设置为导航栏BartinColor您只需使用正确的状态栏向当前视图添加一个UIView测量,然后改变

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

self.navigationController.navigationBar.barTintColor = [UIColor redColor];
但当我隐藏导航栏时,状态栏的颜色将恢复为透明颜色


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

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

您只需使用正确的状态栏向当前视图添加一个
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];

瞧,在self.navigationController?之后设置self.tableView.contentInset=UIEdgeInsetsZero.navigationBarHidden=true
我找到了一个使用InterfaceBuilder解决问题的简单方法。 我的问题是这样的,有一个恼人的白色缺口,一个隐藏后的导航栏

[

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

那就行了

这对我来说很有帮助(用Swift):


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

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

[self.view setBackgroundColor:[UIColor blackColor]]

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

这解决了我的问题:

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

在其后面添加具有相同颜色的子视图可能有助于了解状态栏始终是透明的。在其后面放置
UIView
与在其后面放置
UINavigationBar
没有区别。@nhgrif是的,但状态栏是320 x 20,不同于导航栏的尺寸。你们想看swift吗这个版本?或者代码足够简单,可以转换吗?
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}