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)

其他回答

点击支持文件组(左边顶部-项目名称)。单击“信息”。点击列表之间的+,就像下面的bundle name。并添加“基于视图控制器的状态栏外观”并将其设置为NO。 然后打开AppDelegate.swift,像这样修改:

func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {

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

return true
}

这是它。

(截至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 4.2解决方案与NavigationController

第一步:

打开你的信息。并插入一个名为“View controller based status bar appearance”或UIViewControllerBasedStatusBarAppearance的新键到YES,让每个VC使用自己的状态属性。

第二步

在每个VC中,像这样重写preferredStatusBarStyle属性:

override var preferredStatusBarStyle : UIStatusBarStyle {
    return .lightContent //.default for black style
}

最后一步

重写自定义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
}
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()
    }

}

我遵循了这个教程,它对我很有效。然而,我不确定是否有任何警告。

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