2014-01-14 190 views
3

我知道如何通过这样来改变一个导航栏(和状态栏)的颜色:导航栏隐藏在iOS 7中时,如何更改状态栏的颜色?

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

但是,当我躲在我的导航栏,状态栏的颜色恢复到透明的颜色。

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

+0

在它后面添加一个具有相同色调的子视图 – Maximilian

+0

可能有助于了解状态栏始终是透明的。在它后面放置一个'UIView'与将'UINavigationBar'放在它后面没什么区别。 – nhgrif

+0

@nhgrif是的,这是真的,但状态栏是320×20,不同于导航栏的尺寸。 –

回答

2

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

5

您只需添加一个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]; 

+0

你们想不想看到这样的快速版本?或者代码简单到足以转换? – Pavan

0

设置self.tableView.contentInset = UIEdgeInsetsZeroself.navigationController?.navigationBarHidden = true

0

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

[Before[1]

然后我取消这两个选项

options

然后它的工作原理

after## Heading ##

2

这奏效了,我(斯威夫特):

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

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

1

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

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

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

相关问题