How to change status bar text color immediately in Swift in iOS 13
Status bar color is not global (by default) and if you set that to not ViewControllerBased
, you can't change it anymore. So you need to change set it inside any view you need like this:
var statusBarStyle = UIStatusBarStyle.default { didSet { setNeedsStatusBarAppearanceUpdate() } }
override var preferredStatusBarStyle: UIStatusBarStyle { statusBarStyle }
these two variables help you to change the status bar. Note that you can call setNeedsStatusBarAppearanceUpdate
inside an animation block to make it animatable.
to detect when UserInterfaceStyle
change (and update statusBar color accordingly), all views and viewControllers have delegate function for that. So knowing that:
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
updateStatusBarColor()
}
}
And this is the function:
func updateStatusBarColor() {
switch traitCollection.userInterfaceStyle {
case .unspecified: statusBarStyle = .default
case .light: statusBarStyle = .darkContent
case .dark: statusBarStyle = .lightContent
}
}
Note that:
the ParentViewController defines the statusBarColor
. So if you are using general navigationController
or tabBarController
, A custom class for them with these codes should be enough.
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 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())
Change Status Bar Color in iOS 13?
1st way
You can check iOS version and add custom status bar like this ->
override func viewDidAppear(_ animated: Bool) {
if #available(iOS 13, *) {
let statusBar = UIView(frame: (UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame)!)
statusBar.backgroundColor = .systemBackground
UIApplication.shared.keyWindow?.addSubview(statusBar)
}
}
2nd way
You can customize UINavigationBarAppearance
if #available(iOS 13.0, *) {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
navBarAppearance.backgroundColor = <yourColor>
navigationBar.standardAppearance = navBarAppearance
navigationBar.scrollEdgeAppearance = navBarAppearance
}
Related Topics
Custom Table View Row Action (Image)
Please Add the Host Targets for the Embedded Targets to the Podfile
How to Catch Nsunknownkeyexception in Swift 2.2
Ios-Charts How to Put Uiimage Beside a Point
How to Call the Apple Wallet from iOS App Using Swift
How to Rotate Custom Userlocationannotationview Image Using Mapkit in Swift
Multiple Cells Selected on Scrolling [Reuse Cells Problem]
Binary Operator + Cannot Be Applied to Operands of Type Cgfloat Int
Swift/How to Call Delegate with Popviewcontroller
Increase the Width of Content View Programmatically in Swift
@Fetchrequest + Case-Insensitive Sorting - Swiftui & Coredata
How to Set Up Array for Multi Annotations with Swift
Your Credentials Do Not Allow Access to This Resource Twitter API Error
Change Buttonstyle Modifier Based on Light or Dark Mode in Swiftui
Ksecattrkeytypeec Causing Encryptmessagewithpublickey() to Fail
How to Set the Local Storage Before a Uiwebview Loading Its Initial Request
How to Change a View from Portrait Mode to Landscape Mode and Lock It