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)
我已经设置了特定的颜色(在RGB格式)使用下面的代码在应用程序委托文件:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
. . .
UIApplication.sharedApplication().statusBarHidden = false
UIApplication.sharedApplication().statusBarStyle = .LightContent
let statusBar: UIView = UIApplication.sharedApplication().valueForKey("statusBar") as! UIView
if statusBar.respondsToSelector(Selector("setBackgroundColor:")) {
statusBar.backgroundColor = UIColor.init(red: 0.1, green: 0.27, blue: 0.60, alpha: 1.0)
}
. . .
}
您还需要添加以下关键信息。Plist文件:
查看基于控制器的状态栏外观,布尔值设置为NO
有两种情况:
1.显示导航栏
1)添加1uiviewcontrollerbasedstatusbarappearance /基于视图控制器的状态栏外观到你的信息。Plist, set value为true。
2)在你的自定义NavigationController类中覆盖preferredStatusBarStyle属性:
class NavigationController : UINavigationController {
override var preferredStatusBarStyle : UIStatusBarStyle {
if let topVC = viewControllers.last {
//return the status property of each VC, look at step 2
return topVC.preferredStatusBarStyle
}
return .default
}
3)覆盖preferredStatusBarStyle在你的特定视图控制器:
override var preferredStatusBarStyle : UIStatusBarStyle {
return .lightContent
}
2.隐藏导航栏
1)同上
2)不需要以上第二步,直接执行第三步。
override var preferredStatusBarStyle : UIStatusBarStyle {
return .lightContent
}
对于Xcode 10,你可以创建一个类并把它放在你的viewController类之前,你可以在所有视图控制器中调用这个类,需要一个轻内容状态栏…
class UIViewControllerWithLightStatusBar: UIViewController {
override var preferredStatusBarStyle: UIStatusBarStyle {
return UIStatusBarStyle.lightContent
}
}
现在在下面修改你的viewController类:
class YourViewController: UIViewControllerWithLightStatusBar {
...
}
这就是全部……
我遵循了这个教程,它对我很有效。然而,我不确定是否有任何警告。
https://coderwall.com/p/dyqrfa/customize-navigation-bar-appearance-with-swift
打开你的信息。Plist和set
UIViewControllerBasedStatusBarAppearance设为false。
在AppDelegate.swift中的第一个函数中,它包含didFinishLaunchingWithOptions,设置你想要的颜色。
UIApplication.sharedApplication()。statusBarStyle = UIStatusBarStyle。LightContent
Swift 3更新*
共享。statusbarstyle = .lightContent