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
我怎么能改变一些必要的和特定的视图控制器的状态栏颜色?
在Swift 3.0 Xcode 8中,一切都要简单得多
使用下面的代码在应用程序委托文件,之后
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
插入:
UINavigationBar.appearance().barStyle = .black
UINavigationBar.appearance().barTintColor = UIColor(red: 230, green: 32, blue: 31, alpha: 1.0)
警告
'statusBarStyle'的Setter在iOS 9.0中已弃用:
UIApplication.shared.statusBarStyle = .default
所以我的解决方案是这样的:
从导航控制器做一个扩展:
extension UINavigationController {
open override var preferredStatusBarStyle: UIStatusBarStyle {
if let topViewController = presentedViewController{
return topViewController.preferredStatusBarStyle
}
if let topViewController = viewControllers.last {
return topViewController.preferredStatusBarStyle
}
return .default
}
}
如果你有一个viewController它会有另一个样式而不是app的样式,你可以做这个
var barStyle = UIStatusBarStyle.lightContent
override var preferredStatusBarStyle: UIStatusBarStyle{
return barStyle
}
假设你的app状态样式是。default,你希望这个屏幕是。lightcontent
所以barStyle会把。lightContent作为它的默认值,这将改变状态栏的样式为lightContent,然后确保当viewWillDisappear再次改变barStyle为应用程序状态栏的样式,在我们的例子中是。default。
这对我很有用