I am trying to modify the appearance of my status bar (make text white/ set the Style to "light"). I managed to set the background color by adding this to my AppDelegate.swift file:
let statWindow = UIApplication.shared.value(forKey:"statusBarWindow") as! UIView
let statusBar = statWindow.subviews[0] as UIView
statusBar.backgroundColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0)
However, when I go to change the style of the text of the status bar, even changing this under General > Deployment Info > Status Bar Style (changing this to "Light") does not work.
I also tried to modify the status bar through Info.plist, but there is no field for "View controller-based status bar appearance" (see second image). Also, there is no option for a "light" style under the Status bar style option (see below image):
Status bar style options:
No view controller status bar field:
The step you missed is Info.plist.
Open the info.plist file of your app and set the UIViewControllerBasedStatusBarAppearance to NO (as shown below).
Note : This key can be added if not already present by:
1) Hovering over an existing entry to reveal add/remove icons:
2) Click the plus icon to add a new key/value pair:
3) Paste UIViewControllerBasedStatusBarAppearance
into the key field and set it's value to NO
. Note the key will change to View controller-based status...
when deselected but it's the same thing:
In each UIViewController of your application, you should override preferredStatusBarStyle
property:
override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent }
and eventually, call:
<your controller>.setNeedsStatusBarAppearanceUpdate()
If this statusBar style is throughout all your application, you should make a BaseViewController
class that implement this, and make all you view controllers inherit from BaseViewController.
swift 3
if View controller-based status bar appearance = YES in Info.plist
then use this extension for all NavigationController
extension UINavigationController
{
override open var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
if there is no UINavigationController and only have UIViewController then use Below code:
extension UIViewController
{
override open var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
objective c
create category class
For UIViewController
In UIViewController+StatusBarStyle.h
@interface UIViewController (StatusBarStyle)
@end
In UIViewController+StatusBarStyle.m
#import "UIViewController+StatusBarStyle.h"
@implementation UIViewController (StatusBarStyle)
-(UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
@end
For UINavigationController
In UINavigationController+StatusBarStyle.h
@interface UINavigationController (StatusBarStyle)
@end
In UINavigationController+StatusBarStyle.m
#import "UINavigationController+StatusBarStyle.h"
@implementation UINavigationController (StatusBarStyle)
-(UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
@end
For Xcode 10 You can simply create a class for UIViewController or UITableViewController ecc. and put it before your UIViewController class, you can call this class in all view controller is needed a light content status bar...
class UIViewControllerWithLightStatusBar: UIViewController {
override var preferredStatusBarStyle: UIStatusBarStyle {
return UIStatusBarStyle.lightContent
}
}
in AppDelegate set the status Bar:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().clipsToBounds = false
UINavigationBar.appearance().barStyle = .blackOpaque
return true
}
now you change the class UIViewController with UIViewControllerWithLightStatusBar
class YourViewController: UIViewControllerWithLightStatusBar {
...
}
And that's it...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.