How to change the color of the status bar when the navigation bar is hidden in iOS 7?

I know how to change the color of the navigation bar (and status bar) by following these steps:

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

But when I hide my navigation bar, the color of the status bar goes back to a transparent color.

How to keep the color of the status bar the same as barTintColor, even if the navigation bar is hidden?

+5
source share
7 answers

Add a line UIViewto the status bar and set its property backgroundColorin the navigation barbarTintColor

+2
source

You simply add UIViewto the current view with the correct Bar state measurements, and then change the color.

. :

 //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];

+5

( Swift):

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

, , statusBar. , tableView , .

+2

viewcontrollers. .

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

, .

+2

self.tableView.contentInset = UIEdgeInsetsZero self.navigationController?.navigationBarHidden = true

0

I found an easy way to solve the problem using InterfaceBuilder. My problem is that there is a white space after hiding the navigation bar.

[Before [1]

then I will remove these two options

options

then it works

after ## Title ##

0
source

This fixed my problem:

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
0
source

All Articles