简体   繁体   English

在XCode 8.2 / Swift 3.0中更改状态栏样式(否“查看基于控制器的状态栏外观”)

[英]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: 我设法通过将其添加到我的AppDelegate.swift文件来设置背景颜色:

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. 但是,当我去更改状态栏文本的样式时,即使在常规>部署信息>状态栏样式(将其更改为“Light”)下更改此项也不起作用。

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). 我还尝试通过Info.plist修改状态栏,但没有“查看基于控制器的状态栏外观”的字段(请参阅第二张图像)。 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. 您错过的步骤是Info.plist。

Open the info.plist file of your app and set the UIViewControllerBasedStatusBarAppearance to NO (as shown below). 打开应用程序的info.plist文件,将UIViewControllerBasedStatusBarAppearance设置为NO (如下所示)。

截图

Note : This key can be added if not already present by: 注意 :如果尚未出现此密钥,则可以添加此密钥:

1) Hovering over an existing entry to reveal add/remove icons: 1)将鼠标悬停在现有条目上以显示添加/删除图标:

在此输入图像描述

2) Click the plus icon to add a new key/value pair: 2)单击加号图标以添加新的键/值对:

在此输入图像描述

3) Paste UIViewControllerBasedStatusBarAppearance into the key field and set it's value to NO . 3)将UIViewControllerBasedStatusBarAppearance粘贴到关键字段中,并将其值设置为NO Note the key will change to View controller-based status... when deselected but it's the same thing: 注意,当取消选择键时,键将更改为View controller-based status...但它是相同的:

在此输入图像描述 在此输入图像描述

In each UIViewController of your application, you should override preferredStatusBarStyle property: 在应用程序的每个UIViewController中,您应该覆盖preferredStatusBarStyle属性:

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. 如果这个statusBar样式遍及你的所有应用程序,你应该创建一个实现它的BaseViewController类,并使你所有的视图控制器继承自BaseViewController。

swift 3 迅捷3

if View controller-based status bar appearance = YES in Info.plist 如果在Info.plist中查看基于控制器的状态栏外观= YES

then use this extension for all NavigationController 然后对所有NavigationController使用此扩展

    extension UINavigationController
    {
        override open var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
         }
     }

if there is no UINavigationController and only have UIViewController then use Below code: 如果没有UINavigationController,只有UIViewController,那么使用下面的代码:

    extension UIViewController
    {
        override open var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
         }
     }

objective c 目标c

create category class 创建类别类

For UIViewController 对于UIViewController

In UIViewController+StatusBarStyle.h 在UIViewController + StatusBarStyle.h中

 @interface UIViewController (StatusBarStyle)
 @end

In UIViewController+StatusBarStyle.m 在UIViewController + StatusBarStyle.m中

 #import "UIViewController+StatusBarStyle.h"

 @implementation UIViewController (StatusBarStyle)

 -(UIStatusBarStyle)preferredStatusBarStyle {
  return UIStatusBarStyleLightContent;
 }

 @end 

For UINavigationController 对于UINavigationController

In UINavigationController+StatusBarStyle.h 在UINavigationController + StatusBarStyle.h中

 @interface UINavigationController (StatusBarStyle)
 @end

In UINavigationController+StatusBarStyle.m 在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. 对于Xcode 10您只需为UIViewController或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... 并把它放在你的UIViewController类之前,你可以在所有视图控制器中调用这个类,需要一个轻量级的状态栏...

class UIViewControllerWithLightStatusBar: UIViewController {
override var preferredStatusBarStyle: UIStatusBarStyle {
return UIStatusBarStyle.lightContent
}
}

in AppDelegate set the status Bar: 在AppDelegate中设置状态栏:

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 现在,您使用UIViewControllerWithLightStatusBar更改类UIViewController

class YourViewController: UIViewControllerWithLightStatusBar {
...
}

And that's it... 就是这样......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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