In my iPhone application built with Xcode 5 for iOS 7 I set UIViewControllerBasedStatusBarAppearance=YES
in info.plist
, and in my ViewController
I have this code:
-(UIStatusBarStyle) preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
But the status bar is still black against the black background.
I know its possible to change this app-wide by setting UIViewControllerBasedStatusBarAppearance=NO
in info.plist
, but I actually need to alter this on a viewController
by viewController
basis at runtime.
swift example
in AppDelegate.swift
in info.plist set View controller-based status bar appearance: NO
If you're using
NavigationController
, you can subclassNavigationController
so that it consults its child view controller// MyCustomNavigationController
I just want to add a note for a specific case I faced. I had another UIWindow in my app to display a chat face to be floating all over my app all the time. Doing this caused none of the solution above to work, and I am not really sure why! All what I have noticed is that my ViewController in the new UIWindow was the reason for that! And if I wanted to change the status bar style I have to do it in that view controller of the new UIWindow.
This note might help others who have a similar structure! So basically you can apply the solutions mentioned above in the ViewController of the new UIWindow.
Again this a specific case.
Thanks
For
preferredStatusBarStyle()
to work withinUINavigationController
andUITabBarController
I add the following code, which will get the preferred status bar style from the currently visible view controller.For Swift 3 those are not methods but properties:
The Swift 4.2 properties have been renamed:
Usage
In viewDidLoad just write this
just do that and it will work
can u please try this
One more thing i have seen in your question that you have wrote the method like this
but it should be like this
Even with all the answers here i still didn't find the exact solution for me, but started with the answer from Daniel. What I ended up with was:
in navigation controllers (similar for tab, just selectedViewController). And then it will respect the:
In each view controller unless you set it otherwise. I dont need to call
setNeedsStatusBarAppearanceUpdate()
anywhere, it just updates when you arrive at each view controller.