在iOS8中使用Swift更改特定ViewControllers的状态栏颜色 [英] Changing the Status Bar Color for specific ViewControllers using Swift in iOS8

查看:126
本文介绍了在iOS8中使用Swift更改特定ViewControllers的状态栏颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在任何ViewController中使用上面的代码将statusBar颜色设置为白色特定的viewcontroller 对我来说在iOS8中不起作用 。有什么建议么?使用UIApplication.sharedApplication方法,在整个应用程序的Info.plist中所需的更改后,颜色会发生变化。

Using the above code in any ViewController to set the statusBar color to White for a specific viewcontroller doesnt work in iOS8 for me. Any suggestions? Using the UIApplication.sharedApplication method, the color changes after required changes in the Info.plist for the whole app.

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

如何更改某些必需和 特定ViewControllers 的状态栏颜色?

How can I just make changes to the status bar color for some required and specific ViewControllers?

推荐答案

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

After reading all the suggestions, and trying out a few things, I could get this to work for specific viewcontrollers using the following steps :

第一步:

打开您的info.plist并插入名为查看基于控制器的状态栏外观to NO

Open your info.plist and insert a new key named "View controller-based status bar appearance" to NO

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

通常我们将以下代码放在appli中阳离子(_:didFinishLaunchingWithOptions :)
AppDelegate的方法,

Normally we put the following code in the application(_:didFinishLaunchingWithOptions:) method of the AppDelegate,

Swift 2

Swift 2

UIApplication.sharedApplication().statusBarStyle = .LightContent

Swift 3

Swift 3

UIApplication.shared.statusBarStyle = .lightContent

影响所有ViewControllers的statusBarStyle

那么,如何让这个适用于特定的ViewControllers - 最后一步:

打开要更改 statusBarStyle 的viewcontroller文件,并将以下代码放入 viewWillAppear()

Open the viewcontroller file where you want to change the statusBarStyle and put the following code in viewWillAppear(),

Swift 2

Swift 2

UIApplication.sharedApplication().statusBarStyle = .LightContent

Swift 3

Swift 3

UIApplication.shared.statusBarStyle = .lightContent

此外,实现 viewWillDisappear() 该特定viewController的方法并放入以下代码行,

Also, implement the viewWillDisappear() method for that specific viewController and put the following lines of code,

Swift 2

Swift 2

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

}

Swift 3

Swift 3

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

此步骤将首先更改特定viewcontroller的 statusBarStyle ,然后将其更改回 default 特定的viewcontroller消失了。未实现 viewWillDisappear()会将 statusBarStyle 永久更改为新定义的 UIStatusBarStyle值.LightContent

This step will first change the statusBarStyle for the specific viewcontroller and then change it back to default when the specific viewcontroller disappears. Not implementing the viewWillDisappear() will change the statusBarStyle permanently to the new defined value of UIStatusBarStyle.LightContent

这篇关于在iOS8中使用Swift更改特定ViewControllers的状态栏颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆