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

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


当前回答

有两种情况:

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
}

其他回答

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

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

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]。

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

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

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

return true
}

这是它。

斯威夫特2

通过在viewWillAppear中添加以下内容,我成功地改变了状态栏背景的外观:

let statusBar: UIView = UIApplication.sharedApplication().valueForKey("statusBar") as! UIView

    if statusBar.respondsToSelector(Selector("setBackgroundColor:")) {
        statusBar.backgroundColor = .redColor()
    }

斯威夫特3

在你的AppDelegate文件中的func application方法中

let statusBar: UIView = application.value(forKey: "statusBar") as! UIView
statusBar.backgroundColor = .red

适用于基于导航的应用

    var addStatusBar = UIView()
    addStatusBar.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 20);
    addStatusBar.backgroundColor = global().UIColorFromRGB(0x65b4d9)
    self.window?.rootViewController?.view .addSubview(addStatusBar)