iOS 7 and later: set status bar style per view controller
Have you tried this?
Set "View controller-based status bar appearance" (
UIViewControllerBasedStatusBarAppearance
) toYES
in your Info.plist. (YES
is the default, so you can also just leave this value out of your plist.)In your viewDidLoad method, call
[self setNeedsStatusBarAppearanceUpdate]
.Implement
preferredStatusBarStyle
, returning the status bar style that you want for this view controller.- (UIStatusBarStyle) preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
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
.
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 style during launch on iOS 7
To your Info.plist file add this key-value pair:
UIStatusBarStyle: UIStatusBarStyleLightContent
The default (black) value is UIStatusBarStyleDefault
.
You can also append ~iphone
or ~ipad
to the key.
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())
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).
Related Topics
Change Frame Programmatically with Auto Layout
Uitableviewrowaction Image for Title
How to Get HTML Source from Url with Swift
iOS 11 Navigationitem.Titleview Width Not Set
iOS 11 Large-Title Navigation Bar Not Collapsing
Import Swift Class in Objective-C, <Mymodule>-Swift.H File Not Found
Can't Connect Iboutlet in Interface Builder
Get Date and Time from Apple Server
How to Do Authentication in Uiwebview Properly
iOS Cllocationmanager in a Separate Class
How to Directly Rotate Cvimagebuffer Image in iOS 4 Without Converting to Uiimage
Changing View Controller When Segmented Control Changes
Convert Nsdate to String in iOS Swift
Ios: How to Run a Function After Device Has Rotated (Swift)
Loading Viewcontroller from Xib File
How to Remove Tmp Directory Files of an iOS App
How to Get Notified of Uitableviewcell Move Start and End
iOS - 'Myproject-Swift.H' File Not Found When Running Unit Tests for Swift