I'm using Xcode 8.0 beta 4.
In previous version, UIViewController have method to set the status bar style
public func preferredStatusBarStyle() -> UIStatusBarStyle
However, I found it changed to a "Get ONLY varaiable" in Swift 3.
public var preferredStatusBarStyle: UIStatusBarStyle { get }
How can provide the style to use in my UIViewController?
Here is Apple Guidelines/Instruction about status bar style change.
If you want to set status bar style, application level then set
UIViewControllerBasedStatusBarAppearance
toNO
in your.plist
file. And in yourappdelegate
>didFinishLaunchingWithOptions
add following ine (programatically you can do it from app delegate).Objective C
Swift
if you wan to set status bar style, at view controller level then follow these steps:
UIViewControllerBasedStatusBarAppearance
toYES
in the.plist
file, if you need to set status bar style at UIViewController level only.In the viewDidLoad add function -
setNeedsStatusBarAppearanceUpdate
override preferredStatusBarStyle in your view controller.
Objective C
Swift
Set value of .plist according to status bar style setup level.
This is the preferred method for iOS 7 and higher
In your application's
Info.plist
, set "View controller-based status bar appearance" to YES.Override
preferredStatusBarStyle
in each of your view controllers. For example:If you have
preferredStatusBarStyle
returning a different preferred status bar style based on something that changes inside of your view controller (for example, whether the scroll position or whether a displayed image is dark), then you will want to callsetNeedsStatusBarAppearanceUpdate()
when that state changes.If you use a navigation controller and you want the preferred status bar style of each view controller to be used, see https://stackoverflow.com/a/41026726/1589422.
iOS before version 7, deprecated method
Apple has deprecated this, so it will be removed in the future. Use the above method so that you don't have to rewrite it when the next iOS version is released.
If your application will support In your application's
Info.plist
, set "View controller-based status bar appearance" to NO.In
appDelegate.swift
, thedidFinishLaunchingWithOptions
function, add:If you want to change the status bar style any time after the view has appeared you can use this:
In file info.list add row: View controller-based status bar appearance and set it to YES
If you want to change the
statusBar
's color to white, for all of the views contained in aUINavigationController
, add this insideAppDelegate
:This code:
does not work for
UIViewControllers
contained in aUINavigationController
, because the compiler looks for thestatusBarStyle
of theUINavigationController
, not for thestatusBarStyle
of theViewControllers
contained by it.Hope this helps those who haven't succeeded with the accepted answer!
First step you need add a row with key:
View controller-based status bar appearance
and valueNO
toInfo.plist
file. After that, add 2 functions in your controller to specific only that controller will effect:For objective C just add this line in your application didFinishLaunch method