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
}
}
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
Change Status Bar Background Color in Swift 3
extension UIApplication {
var statusBarView: UIView? {
if responds(to: Selector(("statusBar"))) {
return value(forKey: "statusBar") as? UIView
}
return nil
}
}
UIApplication.shared.statusBarView?.backgroundColor = .red
Update for iOS 13
App called -statusBar or -statusBarWindow on UIApplication: this code
must be changed as there's no longer a status bar or status bar
window. Use the statusBarManager object on the window scene instead.
Refer to How to change the status bar background color and text color on iOS 13?
Status Bar Style - Swift 3 - change at any time
Found the answer after quite a lot of digging!
Set (in info.plist):
View controller-based status bar appearance = NO
and remove the (in ViewController.swift):
override var preferredStatusBarStyle: UIStatusBarStyle {
return UIStatusBarStyle.default
}
...
Now you can use (in ViewController.swift):
UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: true)
And, to initially set the style for each ViewController, use viewDidAppear:
override func viewDidAppear(_ animated: Bool) {
UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: false)
}
How to change status bar style - iOS 12
Set View controller-based status bar appearance
to NO
in the info.plist
and override preferredStatusBarStyle
in each view controller like so:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
And call setNeedsStatusBarAppearanceUpdate()
in your view controller (in viewDidLoad()
for example).
Change Status Bar style in XCode 8.2 / Swift 3.0 (No View controller-based status bar appearance)
In each UIViewController of your application, you should override preferredStatusBarStyle
property:
override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent }
and eventually, call:
<your controller>.setNeedsStatusBarAppearanceUpdate()
If this statusBar style is throughout all your application, you should make a BaseViewController
class that implement this, and make all you view controllers inherit from BaseViewController.
How to change status bar color?
Set your status bar style to dark content:
After that add in your info.plist View controller-based status bar appearance and set it to NO
UPDATE
if you want dark content only in determinate controller add setNeedsStatusBarAppearanceUpdate in viewWillAppear and after that override preferredStatusBarStyle:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
if #available(iOS 13.0, *) {
return .darkContent
} else {
return .default
}
Begin with navigation Controller:
In your Scene delegate declare your first navigation controller:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.makeKeyAndVisible()
let controller = UINavigationController(rootViewController: FirstViewController())
controller.navigationBar.barStyle = .black
window?.rootViewController = controller
}
in SecondViewController override the status bar style
override var preferredStatusBarStyle: UIStatusBarStyle {
if #available(iOS 13.0, *) {
return .darkContent
} else {
return .default
}
}
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: UIHostingControllerZ<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())
How to change the status bar style when a view controller is loading?
The problem you're experiencing is that UINavigationController
doesn't defer the choice of status bar to its view controllers.
Instead, for a navigation controller, the status bar style can be set by adjusting the barStyle
property of its navigationBar
.
If it is set to a black style, then the status bar will be light style:
navigationController?.navigationBar.barStyle = .black
Note, that this will also change the color of the navigationBar
, however you can still set the bar's colour to whatever you want using barTintColor
:
navigationController?.navigationBar.barTintColor = .purple
If you want to make a global change, so that all instances of UINavigationController
use the same status bar style (useful if you've got multiple tabs all of which use a navigation controller), then you can add an extension on UINavigationController
and override the preferredStatusBarStyle
property:
extension UINavigationController {
open override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
A final option, is to defer the choice to the view controllers in the navigation controller's stack.
To do that, override the childViewControllerForStatusBarStyle
property of your navigation controller extension and have it return the topViewController
:
extension UINavigationController {
open override var childViewControllerForStatusBarStyle: UIViewController? {
return topViewController
}
}
In this case, you'll need to override preferredStatusBarStyle
in all of your view controllers (not the optimal approach, but it's an option if you need this granular level of control on a per-child-controller basis).
All of these solutions require that your View controller-based status bar appearance
key in Info.plist is set to YES
.
Related Topics
iPhone X How to Handle View Controller Inputaccessoryview
How to Download Files from Uiwebview and Open Again
Uniquely Identifying an iOS User
In Swiftui, How to Use Uihostingcontroller Inside an Uiview or as an Uiview
Add Glowing Effect to an Skspritenode
Using Uipageviewcontroller with Swift and Multiple View Controllers
Fatal Error: Malformed or Corrupted Ast File - Xcode
How to Manage Cookies with Uiwebview in Swift
Blocks on Swift (Animatewithduration:Animations:Completion:)
Didupdatelocations Instead of Didupdatetolocation
Swift Protocol Get Only Settable
iOS - Integrating Credit Card Payments
Avcapturesession Audio Doesn't Work for Long Videos
How to Draw a Single Point Line in iOS
Storing Uicolor Object in Core Data
Could Not Launch Process Launch Failed: Timed Out Waiting for App to Launch