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
}
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 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.
iOS 13 setting status bar background color
Kuray just provided me a solution here:
https://freakycoder.com/ios-notes-13-how-to-change-status-bar-color-1431c185e845
Add the below to viewdidload. Please head over to his medium post and give him a few claps!
if #available(iOS 13.0, *) {
let app = UIApplication.shared
let statusBarHeight: CGFloat = app.statusBarFrame.size.height
let statusbarView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: statusBarHeight))
statusbarView.backgroundColor = UIColor.red
view.addSubview(statusbarView)
} else {
let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
statusBar?.backgroundColor = UIColor.red
}
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())
Related Topics
Xcode Doesn't Write Anything to Output Console
Setting Layoutmargins of Uiview Doesn't Work
Authentication with Wkwebview in Swift
Ios/C: Convert "Integer" into Four Character String
<Googlemaps/Googlemaps.H> File Not Found Google Maps Sdk for iOS
Uipickerview: Nsattributedstring Not Available in iOS 7
Uisearchcontroller Disable Cancel Uibarbuttonitem
Fontawesome Pro and Xamarin.iOS Only One Font Can Be Active
iOS Nsnotificationcenter to Check Whether the App Came from Background to Foreground
Application Is "Ready to Sale" But Not Reflected on Itunes Store
Xcode 6.3: Could Not Load Nib in Bundle
How to Get Touchid Information and Compare to a Fingerprint Database
Get Each Line of Text in a Uilabel
iOS Permission Alerts - Removing or Suppressing
Application Identifier Entitlement Value Has Changed
Enable Uialertaction of Uialertcontroller Only After Input Is Validated