how do I properly change my status bar style in swift 2/ iOS 9?
Solution 1
Apple have added the capability to change the status bar style in the deployment info. Simply choose 'Light'.
Also set View controller-based status bar appearance
key to NO
in the Info.plist
Solution 2
I always did this way.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
//Changing Status Bar
override func preferredStatusBarStyle() -> UIStatusBarStyle {
//LightContent
return UIStatusBarStyle.LightContent
//Default
//return UIStatusBarStyle.Default
}
}
It works in any swift 2.x version. This requires that you set View controller-based status bar appearance
in your Info.plist
file to YES
.
Solution 3
Swift 3 just add View controller-based status bar appearance
with value NO
to info.plist
and then add to ViewController
where you want:
UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
Solution 4
You can still use preferredStatusBarStyle
in your view controller:
step 1: in the info.plist set ViewControllerBasedStatusBarAppearance to YES. step 2: add this code to the ViewController you'd like to edit :
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
*** Tip: It seems to only work outside of the ViewDidLoad(), didReceiveMemoryWarning() functions.
Solution 5
The change in deployment info works but despite - you need to add the 'View controller-based status bar appearance' key to plist file setting it to NO.
Related videos on Youtube
John Jackson
Updated on March 16, 2020Comments
-
John Jackson about 4 years
I'm attempting to change my status bar's style to
.Light
but the previous code I implemented in swift 1.2 seems not to work anymore.. here's the code:override func viewDidLoad() { super.viewDidLoad() UIApplication.sharedApplication().statusBarStyle = .LightContent }
now I have my
View controller-based status bar appearance
info.plist setting to YES, and reading the UIKit doc, this will negate any statusBarStyle changes and keep it at default. However when I change the setting to 'NO' and change the statusBarStyle, I get this<Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable
in my debugger.. So is this a bug in Xcode? because to change the status bar style you must change info.plist setting to NO, but when that happens.. error -
Alexander Volkov over 8 yearsThis not help if the content is changed so that it required to change the status bar for the same view controller programmatically.
-
real 19 over 8 yearsPlease also make sure if you use this method that you set the flag in info.plist file to make the view controller based status bar flag to yes
-
Ethan Mick over 8 yearsThis did not change it for me, XCode 7, iOS 9.2
-
Jack Berstrem about 8 years@real19 Yup, worked for me. Just to clear things up for beginners. To do this, just add a property in the Info.plist file, called "View controller-based status bar appearance" (Xcode will auto-complete the property name, and automatically recognize that it is a boolean), and set this property to "YES". This will make the status bar have a "light content" effect.
-
TomSawyer about 8 yearswhat if i want to change it manually or base on the context? i have a drop down notification and when this drop down shows up, i want to change status bar color. is it possible?
-
Aleksander about 8 yearsPlease note that this only works if you set the
View controller-based status bar appearance
key toNO
in theInfo.plist
-
Doughnut Man almost 8 yearsit doesn't work when there is the navigation bar. it only works without it
-
Jayprakash Dubey almost 8 yearsIf you select Light in 'Status Bar style' in project settings then it should automatically add this key in Info.plist file.
-
javal88 over 7 yearsYeah but it's not always working and i don't know why
-
Max Rogers over 7 yearsin iOS 10, overriding preferredStatusBarStyle doesn't appear to be enough. Need to be declared using UIApplication.shared.statusBarStyle
-
Beau Nouvelle over 7 years@AlessandroLucarini Can you try this and report back?
navigationController?.navigationBar.barStyle = .Default
The above answer may work when the VC isn't embedded in a Navigation stack. -
Beau Nouvelle over 7 years@DoughnutMan does
navigationController?.navigationBar.barStyle = .Default
work for you? -
Anirudh R.Huilgol. over 7 years@Beau Nouvelle navigationController?.navigationBar.barStyle = .Default this will change the navigation bar style not status bar style.
-
javal88 over 7 years@BeauNouvelle i've solved extending NavigationController by overriding
childViewControllerForStatusBarStyle
when my controller is embedded in a navigation controller and by setmodalPresentationCapturesStatusBarAppearance
when vc is presented modally. -
Beau Nouvelle over 7 yearsWhich I believe also changes the status bar under certain circumstances.
-
Beau Nouvelle over 7 yearsWhat a freaking hassle to get this working. I'm having similar issues with some legacy code. I need to perform a blood ritual to get this stuff to work. It should really only be a single line of code. Apple....
-
dfrobison about 7 yearsIt should but it doesn't seem to work that way. I had to set the Info.plist key and then it worked.
-
Joney Spark almost 7 yearsI can now change the status bar style at will after having implemented suggested method above in the view controller, then calling
self.setNeedsStatusBarAppearanceUpdate()
from the same controller anywhere in the code. The preferredStatusBarStyle() checks if the UI uses dark or light theme and basically returns the opposite style for the status bar. Perfect.