简体   繁体   中英

Change Status Bar style in XCode 8.2 / Swift 3.0 (No “View controller-based status bar appearance”)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM