UIStatusBarStyle not working in Swift

前端 未结 11 491
轻奢々
轻奢々 2020-12-22 18:30

I\'m trying to change the Status Bar color in my Swift app to white, but am hitting a brick wall. I have 3 ViewControllers that are each embedded in a NavigationController (

相关标签:
11条回答
  • 2020-12-22 19:27

    For iOS9.x and Xcode7, just put this inside AppDelegate.swift:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
        UINavigationBar.appearance().barStyle = .Black
    
    } 
    

    This will automatically turn your status bar's style to .Lightcontent for all the view controllers inside a UINavigationController.

    (Also, delete View controller-based status bar appearance from Info.plist to suppress the warnings you're probably seeing too!)

    0 讨论(0)
  • 2020-12-22 19:31

    In Swift 3, status bar style has changed to a computed property in UIViewController that you can override like this:

    override var preferredStatusBarStyle: UIStatusBarStyle {
       return .lightContent //or default
    } 
    
    0 讨论(0)
  • 2020-12-22 19:32

    Swift 3.0

    in AppDelegate.swift didFinishLaunchingWithOptions

    UIApplication.shared.statusBarStyle = .lightContent
    

    Info.plist

    View controller-based status bar appearance -> NO
    

    Swift 2.2

    in AppDelegate.swift didFinishLaunchingWithOptions

    UIApplication.sharedApplication().statusBarStyle = .LightContent
    

    Info.plist

    View controller-based status bar appearance -> NO
    
    0 讨论(0)
  • 2020-12-22 19:32

    You have to set the:

    navigationController.navigationBar.barStyle = .black

    and the text will appear in white

    0 讨论(0)
  • 2020-12-22 19:34

    You have two options.

    If you want to continue manually setting the style of the status bar, continue doing what you're doing, but you'll need to add the following key to your info.plist file with a value of NO.

    View controller-based status bar appearance

    Or, if you want to continue to use view controller based status bar appearance, instead of setting the application's statusBarStyle, override the preferredStatusBarStyle property in each view controller for which you'd like to specify a status bar style.

    Swift 3

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    

    Swift 2

    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return UIStatusBarStyle.LightContent
    }
    
    0 讨论(0)
提交回复
热议问题