override func preferredStatusBarStyle() -> UIStatusBarStyle {
 return UIStatusBarStyle.LightContent;
}

使用上述代码在任何ViewController中为特定的ViewController设置状态栏颜色为白色,在iOS8中对我来说是行不通的。有什么建议吗?使用UIApplication。shared应用方法,在信息中需要更改后颜色更改。Plist为整个应用程序。

// Change the colour of status bar from black to white
UIApplication.sharedApplication().statusBarStyle = .LightContent

我怎么能改变一些必要的和特定的视图控制器的状态栏颜色?


当前回答

在阅读了所有的建议,并尝试了一些事情之后,我可以使用以下步骤让它为特定的视图控制器工作:

第一步:

打开你的信息。并插入一个名为“View controller based status bar appearance”的新键到NO

第二步(只是一个解释,不需要实现这个):

通常我们在应用程序中放入以下代码(_:didFinishLaunchingWithOptions:) 方法,

斯威夫特2

UIApplication.sharedApplication().statusBarStyle = .LightContent

斯威夫特3

UIApplication.shared.statusBarStyle = .lightContent

但那会影响所有viewcontroller的statusBarStyle。

那么,如何让它为特定的viewcontroller工作-最后一步:

打开你想要更改statusBarStyle的视图控制器文件,并将以下代码放在viewWillAppear()中,

斯威夫特2

UIApplication.sharedApplication().statusBarStyle = .LightContent

斯威夫特3

UIApplication.shared.statusBarStyle = .lightContent

同样,为特定的viewController实现viewWillDisappear()方法,并放入以下代码行:

斯威夫特2

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default

}

斯威夫特3

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
}

这一步将首先更改特定视图控制器的statusBarStyle,然后在特定视图控制器消失时将其更改回默认值。不实现viewWillDisappear()将永久性地将statusBarStyle更改为UIStatusBarStyle的新定义值。LightContent

其他回答

适用于基于导航的应用

    var addStatusBar = UIView()
    addStatusBar.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 20);
    addStatusBar.backgroundColor = global().UIColorFromRGB(0x65b4d9)
    self.window?.rootViewController?.view .addSubview(addStatusBar)
override func viewWillAppear(animated: Bool) {
    self.navigationController?.navigationBarHidden =  true

    UIApplication.sharedApplication().statusBarHidden = false
    UIApplication.sharedApplication().statusBarStyle = .LightContent

    let statusBar: UIView = UIApplication.sharedApplication().valueForKey("statusBar") as! UIView
    if statusBar.respondsToSelector("setBackgroundColor:") {
        statusBar.backgroundColor = UIColor.redColor()
    }

}

实现preferredStatusBarStyle,如你所述,并调用self.setNeedsStatusBarAppearanceUpdate()在ViewDidLoad和 也在信息。设置UIViewControllerBasedStatusBarAppearance为YES(默认是YES)

目前还不清楚为什么它不起作用。我需要检查代码。另一个建议是 使用viewDidLoad UIApplication.sharedApplication()中的工作代码。statusBarStyle = . lightcontent,并将此更改为默认值,当您查看viewgetdisappear viewWillDisappear。

斯威夫特3

在你的AppDelegate文件中的func application方法中

let statusBar: UIView = application.value(forKey: "statusBar") as! UIView
statusBar.backgroundColor = .red

我在Swift 5、Swift 4.2中使用这种方式。

在Info.plist中添加下一个值:

UIViewControllerBasedStatusBarAppearance = YES

or

UIViewControllerBasedStatusBarAppearance = NO(查看变化)

UIStatusBarHidden = NO

UIStatusBarStyle = UIStatusBarStyleDefault(或设置为UIStatusBarStyleLightContent,如果你想在启动时看到轻状态栏文本)

然后将代码放置到您想要看到浅色内容的特定视图控制器(要看到深色文本,请将preferredStatusBarStyle设置为. darkcontent)。

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

override func viewDidLoad() {
    super.viewDidLoad()

    if let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as? UIView {
        statusBar.backgroundColor = .sunflowerYellow
    }
}