Navigating the intricacies of iOS development often involves meticulous attention to detail, and one such detail that significantly impacts user experience is the status bar. The status bar, typically found at the top of an iPhone or iPad screen, provides crucial information like time, Wi-Fi signal, and battery life. Its appearance, whether light or dark, needs to seamlessly integrate with your app’s visual design. For developers working with older projects or maintaining specific application environments, understanding how to set Status Bar Style in Swift 3 remains a highly relevant skill. This guide will walk you through the essential steps and best practices to ensure your appβs status bar aligns perfectly with your aesthetic vision, enhancing both usability and visual consistency for your users.
Understanding UIStatusBarStyle in Swift 3
In Swift 3, managing the status bar’s appearance primarily revolves around the UIStatusBarStyle enumeration. This enum defines two main styles: .default and .lightContent. The .default style renders the status bar content in a dark color, suitable for light-colored backgrounds, while .lightContent displays the content in white, ideal for dark backgrounds. Choosing the correct style is crucial for ensuring the status bar remains legible against your app’s various view controller backgrounds.
Before diving into implementation, it’s vital to grasp how iOS determines which status bar style to apply. By default, iOS 9 and later prioritize a setting in your app’s Info.plist file: “View controller-based status bar appearance.” When this key (UIViewControllerBasedStatusBarAppearance) is set to YES (which is the default for new projects), individual view controllers gain control over their status bar style. If it’s set to NO, the status bar style is controlled globally by a different Info.plist key, UIStatusBarStyle, and remains uniform throughout the app. For most modern applications, allowing view controllers to manage their own status bar style provides greater flexibility and a more dynamic user interface.
For instance, imagine an application with a dark-themed login screen and a light-themed dashboard. If “View controller-based status bar appearance” is enabled, you can easily set the login screen’s status bar to .lightContent and the dashboard’s to .default, ensuring optimal readability in both contexts. This flexibility is a cornerstone of creating polished iOS applications, as highlighted by Apple’s Human Interface Guidelines, which emphasize adaptability in design elements like the status bar. According to the Apple Developer Documentation on System Colors, consistency and readability are paramount.
Implementing Status Bar Style in Individual View Controllers
To control the status bar style on a per-view controller basis in Swift 3, you’ll primarily override the preferredStatusBarStyle computed property within your UIViewController subclasses. This method is called by the system whenever it needs to determine the status bar style for the currently presented view controller. By returning either .default or .lightContent, you explicitly tell iOS how the status bar should appear when that specific view controller is active.
Here’s how you would typically implement this:
import UIKit class MyDarkBackgroundViewController: UIViewController { override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent // For dark backgrounds } override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = .darkGray // Example dark background } } class MyLightBackgroundViewController: UIViewController { override var preferredStatusBarStyle: UIStatusBarStyle { return .default // For light backgrounds } override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = .white // Example light background } }
When you transition between MyDarkBackgroundViewController and MyLightBackgroundViewController, the status bar will automatically animate its change to match the preferred style of the new view controller. If you need to force a refresh of the status bar style after a visual change within the same view controller, you can call setNeedsStatusBarAppearanceUpdate(). This method signals to the system that it should re-query the preferredStatusBarStyle property. It’s particularly useful if your view controller’s background color or theme changes dynamically.
For example, if a user toggles a “dark mode” switch within a single view controller, you would update the background, then call setNeedsStatusBarAppearanceUpdate() to ensure the status bar style updates accordingly. This method is part of the UIViewController class, making it readily available for all your view controllers. This granular control is vital for creating a fluid and responsive user interface, allowing developers to craft experiences that adapt seamlessly to different visual contexts within the same application. This approach ensures optimal legibility and user comfort, a key aspect of modern iOS app design, as detailed by numerous iOS development best practices guides.
Handling Status Bar Style with UINavigationController
When your view controllers are embedded within a UINavigationController, the standard approach of overriding preferredStatusBarStyle in individual view controllers might not work as expected. This is because a navigation controller itself is a container view controller, and by default, it dictates the status bar style for its entire hierarchy. In Swift 3, the navigation controller’s childForStatusBarStyle property plays a crucial role in delegating this responsibility.
To allow your presented view controller (the top view controller in the navigation stack) to control the status bar style, you need to subclass UINavigationController and override its preferredStatusBarStyle property, delegating the call to its top view controller. Hereβs a common pattern:
import UIKit class CustomNavigationController: UINavigationController { override var preferredStatusBarStyle: UIStatusBarStyle { return topViewController?.preferredStatusBarStyle ?? .default } // Optional: If you need to force update the status bar style override var childForStatusBarStyle: UIViewController? { return topViewController } }
Question & Answer :
I’m using Xcode 8.0 beta 4.
In previous version, UIViewController have method to set the status bar style
public func preferredStatusBarStyle() -> UIStatusBarStyle
However, I found it changed to a “Get ONLY varaiable” in Swift 3.
public var preferredStatusBarStyle: UIStatusBarStyle { get }
How can provide the style to use in my UIViewController?
[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 } }