Swift version: 5.10
As of iOS 7.0, all view controllers set their own status bar style by default, which means they can have black text or white text depending on what looks best for your view controller. If you want to have light text in the status bar, add this code to your view controller:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
If you want to change the status bar color dynamically, you should call setNeedsStatusBarAppearanceUpdate()
on your view controller, which will force preferredStatusBarStyle
to be read again. Pro tip: you can put setNeedsStatusBarAppearanceUpdate()
inside an animation block to have the change animate.
SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Available from iOS 7.0
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.