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

Enr*_*tyo 5 uikit ios ios7 ios7-statusbar

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

self.navigationController.navigationBar.barTintColor = [UIColor redColor];
Run Code Online (Sandbox Code Playgroud)

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

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

Pav*_*van 6

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

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

 //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
Run Code Online (Sandbox Code Playgroud)

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

UIView *statusBarUnderLay = [[UIView alloc] initWithFrame:[self statusBarFrameViewRect:self.view];
[statusBarUnderLay setBackgroundColor:[UIColor yellow]];
[self.view addSubview:statusBarUnderLay];
Run Code Online (Sandbox Code Playgroud)


vik*_*lla 6

这对我有用(在 Swift 中):

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

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


Max*_*ral 2

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