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

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


当前回答

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

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

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

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

其他回答

警告


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

这对我很有用

对于Xcode 10,你可以创建一个类并把它放在你的viewController类之前,你可以在所有视图控制器中调用这个类,需要一个轻内容状态栏…

class UIViewControllerWithLightStatusBar: UIViewController {
override var preferredStatusBarStyle: UIStatusBarStyle {
return UIStatusBarStyle.lightContent
}
}

现在在下面修改你的viewController类:

class YourViewController: UIViewControllerWithLightStatusBar {
...
}

这就是全部……

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

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

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

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

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

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