5

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?

Enrico Susatyo
  • 19,372
  • 18
  • 95
  • 156

7 Answers7

6

You simply add a UIView to the current view with the correct statusBar measurements and then change the color.

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:

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

and voila

karlingen
  • 13,800
  • 5
  • 43
  • 74
Pavan
  • 17,840
  • 8
  • 59
  • 100
  • Would you guys want to see a swift version of this? Or is the code simple enough to convert? – Pavan Aug 06 '17 at 02:51
6

This did the trick for me (in 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. This resulted in my tableView cells showing behind the statusBar as they were scrolled.

vikzilla
  • 3,998
  • 6
  • 36
  • 57
2

Add a UIView under the status bar and set its backgroundColor property to the navigation bars barTintColor

Maximilian Litteral
  • 3,059
  • 2
  • 31
  • 41
2

My solution was simple setting the background color of the viewcontrollers' view. e.g.

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

Adding a view needs to be handled for cases when the OS chooses not to show the status bar.

dev_exo
  • 164
  • 1
  • 8
1

I found a simple way to solve the problem using InterfaceBuilder. My problem is like this, there is an annoy white gap, after a hide the navigation bar.

[Before[1]

then I uncheck these two options

options

then It works

after## Heading ##

wgr
  • 513
  • 4
  • 9
0

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

akira108
  • 330
  • 3
  • 18
0

This fixed my issue:

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
david72
  • 7,151
  • 3
  • 37
  • 59