How we can set the Light Content style of Status Bar in iOS 9 for whole application?
You can save yourself a lot of work by using a UINavigationController and setting its navigation bar's barStyle
to .Black
. You only have to do this once; you can do it in the storyboard editor.
This will automatically turn your status bar's style to .LightContent
as long as this UINavigationController is in command.
How to set Status Bar Style in Swift 3
[UPDATED] For Xcode 10+ & Swift 4.2+
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
(Apple docs) in each of your view controllers. For example:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
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 call setNeedsStatusBarAppearanceUpdate()
when that state changes.
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
, the didFinishLaunchingWithOptions
function, add:
UIApplication.shared.statusBarStyle = .lightContent
For Navigation Controller
If you use a navigation controller and you want the preferred status bar style of each view controller to be used and set View controller-based status bar appearance
to YES
in your application's info.plist
extension UINavigationController {
open override var preferredStatusBarStyle: UIStatusBarStyle {
return topViewController?.preferredStatusBarStyle ?? .default
}
}
Is it possible to change Status Bar color for all view controllers?
Set the style of the status bar in AppDelegate.swift
:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarStyle = .lightContent
return true
}
And add the following code to your Info.plist
:
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
How to change Status Bar text color in iOS
Set the
UIViewControllerBasedStatusBarAppearance
toYES
in the .plist file.In the
viewDidLoad
do a[self setNeedsStatusBarAppearanceUpdate];
Add the following method:
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
Note: This does not work for controllers inside UINavigationController
, please see Tyson's comment below :)
Swift 3 - This will work controllers inside UINavigationController
. Add this code inside your controller.
// Preferred status bar style lightContent to use on dark background.
// Swift 3
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Swift 5 and SwiftUI
For SwiftUI create a new swift file called HostingController.swift
import Foundation
import UIKit
import SwiftUI
class HostingController: UIHostingController<ContentView> {
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
Then change the following lines of code in the SceneDelegate.swift
window.rootViewController = UIHostingController(rootView: ContentView())
to
window.rootViewController = HostingController(rootView: ContentView())
statusBarStyle in Swift 4
For Specific ViewController Class:
In your viewcontroller class use this code below viewDidLoad() :
override var preferredStatusBarStyle : UIStatusBarStyle {
return .lightContent
}
this will show your status bar in light content
For Whole App:
Set "View controller-based status bar appearance" Key with Value "NO" in your product's Info.plist
Then Use Following code in AppDelegate.Swift inside "didFinishLaunchingWithOptions":
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UIApplication.shared.statusBarStyle = .lightContent
return true
}
Changing the Status Bar Color for specific ViewControllers using Swift in iOS8
After reading all the suggestions, and trying out a few things, I could get this to work for specific viewcontrollers using the following steps :
First Step:
Open your info.plist and insert a new key named "View controller-based status bar appearance" to NO
Second Step (Just an explanation, no need to implement this):
Normally we put the following code in the application(_:didFinishLaunchingWithOptions:)
method of the AppDelegate,
Swift 2
UIApplication.sharedApplication().statusBarStyle = .LightContent
Swift 3
UIApplication.shared.statusBarStyle = .lightContent
but that affects the statusBarStyle
of all the ViewControllers.
So, how to get this working for specific ViewControllers - Final Step:
Open the viewcontroller file where you want to change the statusBarStyle
and put the following code in viewWillAppear()
,
Swift 2
UIApplication.sharedApplication().statusBarStyle = .LightContent
Swift 3
UIApplication.shared.statusBarStyle = .lightContent
Also, implement the viewWillDisappear()
method for that specific viewController and put the following lines of code,
Swift 2
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default
}
Swift 3
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
}
This step will first change the statusBarStyle
for the specific viewcontroller and then change it back to default
when the specific viewcontroller disappears. Not implementing the viewWillDisappear()
will change the statusBarStyle
permanently to the new defined value of UIStatusBarStyle.LightContent
Related Topics
How to Launch My Settings Bundle from My Application
Capture Location in All States App
Changing Iphone's Date and Time
How to Ignore Certain Uitouch Points in Multitouch Sequence
How to Sort 1 Array in Swift/Xcode and Reorder Multiple Other Arrays by the Same Keys Changes
Uiview Drawrect: Draw the Inverted Pixels, Make a Hole, a Window, Negative Space
Searching Through Child Values Firebase/Swift
Fbsopenapplicationerrordomain Code=3
Auto-Implement Swift Protocol Methods in Xcode
None of Your Accounts Are a Member, Code Signing Errors After Upgrading to Xcode 8
Undefined Symbols for Architecture Armv7 for Cocoapods Libraries
Facebook Sdk - iOS - Fail to Share Url (Error 102)
Removing iPad Support from App
How to Detect When User Used Password Autofill on a Uitextfield