9

I want white status bar in my app. For this I set View controller-based status bar appearance to NO and Status bar style to UIStatusBarStyleLightContent. But now I need to hide status bar in some view controllers. To hide it I have to set View controller-based status bar appearance to YES and add - (BOOL)prefersStatusBarHidden {return YES;}. But status bar is black now. It's black when View controller-based status bar appearance is YES and white if NO. So the question is, how to set white status bar and hide it?

UPD: code in VC that I want to have white status bar (prefferdSTatusBarStyle not called)

enter image description here

code in VC with hidden status bar

enter image description here

.plist settings

enter image description here

Result is black status bar, that hides in some VC

UPD2:

I know it's bad to use deprecated methods but with [[UIApplication sharedApplication] setStatusBarHidden:YES]; everything works as I want. If anyone have better solution please let me know.

Ossir
  • 493
  • 1
  • 8
  • 19

5 Answers5

5

This is the swift version:

To hide the status bar or change it's appearance, you need to override the following properties in your view controller itself

override var prefersStatusBarHidden: Bool{
        return true
}

the above hides the status bar and below if you want to set it to white:

override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
}
Shawn Frank
  • 4,381
  • 2
  • 19
  • 29
2

In your plist file add View controller-based status bar appearance Bool property and set it to YES.

Now in your view controller add the methods like below:

// TO MAKE STATUS BAR WHITE
override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return .LightContent
}

// TO MAKE STATUS BAR BLACK
override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return .LightContent
}

// RETURN TRUE TO HIDE AND FALSE TO SHOW STATUS BAR
override func prefersStatusBarHidden() -> Bool {
        return true
}

For Objective-C

- (BOOL)prefersStatusBarHidden {
    return NO;
}

-(UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

For removing redundant code you can make a BaseViewController as subclass of UIViewController and add the methods in that class. And override the method in the class which requires change.

Parth Adroja
  • 13,198
  • 5
  • 37
  • 71
0

if your viewcontroller is embedded in UInavigationController then try writing this code in your

-(BOOL)prefreStatusBarHidden
{
       return [self.navigationController prefersStatusBarHidden];
}
0

you can set using xcode status bar style is "light"

enter image description here

Birendra
  • 623
  • 1
  • 5
  • 17
  • you have do any changes in info.plist file for status bar ? – Birendra Jan 16 '17 at 07:40
  • only `View controller-based status bar appearance` set to `YES` and `Status bar style` set to `UIStatusBarStyleLightContent`. but status bar is black now – Ossir Jan 16 '17 at 07:48
  • you have to set View controller-based status bar appearance set to "NO" – Birendra Jan 16 '17 at 07:51
  • if I set in to `NO` I will not be able to hide status bar with `prefersStatusBarHidden` – Ossir Jan 16 '17 at 07:55
  • you have try this code ? - (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; } – Birendra Jan 16 '17 at 07:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/133245/discussion-between-birendra-and-ossir). – Birendra Jan 16 '17 at 08:02
-1

You can do this by setting the navigation background image in your base viewcontroller.

UIImage *bgImage = [UIImage imageNamed:@"bg_navigationbar"];
[self.navigationController.navigationBar setBackgroundImage:bgImage forBarMetrics:UIBarMetricsDefault];
Lewanny
  • 1
  • 1