简体   繁体   English

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

[英]How do I change the colour of the status bar when the navigation bar is hidden in iOS 7?

I am aware of how to change the colour of a navigation bar (and status bar) by doing this: 我知道如何通过执行以下操作来更改导航栏(和状态栏)的颜色:

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

But when I hide my navigation bar, the status bar color reverts back to transparent color. 但是当我隐藏导航栏时,状态栏颜色将恢复为透明色。

How do I keep the status bar color the same as the barTintColor even when the navigation bar is hidden? 即使导航栏被隐藏,如何使状态栏颜色与barTintColor保持一致?

You simply add a UIView to the current view with the correct statusBar measurements and then change the color. 您只需使用正确的statusBar测量值将UIView添加到当前视图,然后更改颜色。

Here's some sample code. 这是一些示例代码。 First get the status bar's 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

Then create your view in the viewDidload method or whenever you hide your navigation bar with the following code: 然后在viewDidload方法中或使用以下代码隐藏导航栏时创建视图:

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

and voila

This did the trick for me (in Swift): 这对我来说很有用(在Swift中):

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

My problem was I had set my navigationBar to be hidden, which left the statusBar's background to be clear. 我的问题是我将导航栏设置为隐藏,这使得statusBar的背景变得清晰。 This resulted in my tableView cells showing behind the statusBar as they were scrolled. 这导致我的tableView单元格在滚动时显示在statusBar后面。

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

My solution was simple setting the background color of the viewcontrollers' view. 我的解决方案很简单,设置viewcontrollers视图的背景颜色。 eg 例如

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

Adding a view needs to be handled for cases when the OS chooses not to show the status bar. 当操作系统选择不显示状态栏时,需要处理添加视图的情况。

I found a simple way to solve the problem using InterfaceBuilder. 我找到了一种使用InterfaceBuilder解决问题的简单方法。 My problem is like this, there is an annoy white gap, after a hide the navigation bar. 我的问题是这样的,在隐藏导航栏后,有一个恼人的白色差距。

[ [ 前[1]

then I uncheck these two options 然后我取消选中这两个选项

选项

then It works 那它有效

在## Heading ##之后

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

This fixed my issue: 这解决了我的问题:

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM