Color text of status bar in XCode 6-b3 (Swift)

后端 未结 5 2058
闹比i
闹比i 2021-02-01 06:32

I\'ve try to modify status bar color text but no one answer from this thread doesn\'t work. Any especially for XCode 6?

I\'ve tried insert:

override func         


        
相关标签:
5条回答
  • 2021-02-01 07:07

    Be sure to set the View controller-based status bar appearance in your info.plist file to Yes.

    Furthermore, if you are in a UINavigationController, you cannot simply set the style in ViewControllers in it. Subclass the UINavigationController and add this to it:

    override func preferredStatusBarStyle() -> UIStatusBarStyle {
    
        if let vc = self.viewControllers?.last as? UIViewController {
            return vc.preferredStatusBarStyle()
        }
    
        return super.preferredStatusBarStyle()
    }
    

    Now you can set the bar style in the UIViewController subclass and the UINavigationController will listen to it :).

    0 讨论(0)
  • 2021-02-01 07:10

    In your Info.plist you need to define View controller-based status bar appearance to any value.

    enter image description here

    If you define it YES then you should override preferredStatusBarStyle function in each view controller.

    If you define it NO then you can set style in AppDelegate using

    UIApplication.sharedApplication().statusBarStyle = .LightContent

    0 讨论(0)
  • 2021-02-01 07:16

    Just set " View controller-based status bar appearance == NO " in to your plist and put a single line in your appdelegate class in didfinshLaunching method.

     UIApplication.sharedApplication().statusBarStyle = .LightContent
    
    0 讨论(0)
  • 2021-02-01 07:24

    Swift 3.0

    Just set View controller-based status bar appearance == NO in to your *.plist and put below code in your appdelegate class in didFinishLaunchingWithOptions method before return.

    let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
    if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
        statusBar.backgroundColor = UIColor.red
    }
    UIApplication.shared.statusBarStyle = .lightContent
    

    You can change backgroundColor and statusBarStyle as per your requirement.

    0 讨论(0)
  • 2021-02-01 07:29

    Keenle is right on, from iOS7 onward, you have to opt out of viewController-based status bar styles before you can set it app-wide.

    doc:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html#//apple_ref/occ/instm/UIApplication/setStatusBarStyle:animated:

    "To opt out of the view controller-based status bar appearance behavior, you must add the UIViewControllerBasedStatusBarAppearance key with a value of NO to your app’s Info.plist file, but doing so is not recommended."

    0 讨论(0)
提交回复
热议问题