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

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


当前回答

我已经设置了特定的颜色(在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

其他回答

在我的情况下,我使用故事板来组织我的视图控制器。我想改变所有的状态栏风格。

你可以在下面的图片中看到。

Stars View Controller是一个CPBaseNavigationController,而CPBaseNavigationController是UINavigationController的子类。

我试着做下面的步骤:

在AppDelegate.swift func didFinishLaunchingWithOptions中添加 //更改状态栏颜色 UIApplication.sharedApplication()。statusBarHidden = false UIApplication.sharedApplication()。statusBarStyle = .LightContent 但没有效果。 在StoryBoard中,找到Base Tab BarController(上图)。选择属性检查器,将状态条属性更改为轻内容。太糟糕了,没有效果。

最后我明白了。在我的自定义导航控制器CPBaseNavigationController中添加func preferredStatusBarStyle override func preferredStatusBarStyle() -> UIStatusBarStyle { 返回.LightContent } 它工作得很好!

此外,statusBarStyle在9.0中已弃用,你可以使用-[UIViewController preferredStatusBarStyle]。

适用于swift4中基于特定视图控制器的导航

   let app = UIApplication.shared
   let statusBarHeight: CGFloat = app.statusBarFrame.size.height

   let statusbarView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: statusBarHeight))
   statusbarView.backgroundColor = UIColor.red
   view.addSubview(statusbarView)

(截至2020年6月10日)

Swift 5(不编辑。plist文件)

如果你正在使用故事板,转到导航控制器,选择导航栏,单击属性检查器,然后更改样式。如果你需要浅色内容(白色状态栏),设置它除了默认设置样式黑色,如果你想要深色内容(黑色状态栏),设置它默认。

默认的(UIBarStyleDefault)导致前景UIStatusBarStyleDefault状态栏为黑色。UIBarStyleBlack会给出一个uistatusbarstyelightcontent状态栏。

编程

let nav = UINavigationController(rootViewController: rootViewController)

    nav.navigationBar.barStyle = .default //gives you dark Content status bar

    nav.navigationBar.barStyle = .black  //gives you light content status bar

没有导航栏(编辑。plist)

添加UIViewControllerBasedStatusBarAppearance /基于视图控制器的状态栏外观到你的信息。Plist, set value为true。

重写控制器中的preferredStatusBarStyle属性

class ViewController: UIViewController {
    override var preferredStatusBarStyle : UIStatusBarStyle {
        return .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)

在你的信息。plist你需要将基于视图控制器的状态栏外观定义为任何值。

如果你定义它为YES,那么你应该在每个视图控制器中重写preferredStatusBarStyle函数。

如果你定义它为NO,那么你可以在AppDelegate中使用

UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)