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

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


当前回答

另一种非常简单的方法是创建UINavigationController类的扩展。

因为重写preferredStatusBarStyle:方法不会工作,除非我们在UINavigationController类内部做它。

    extension UINavigationController {
        open override var preferredStatusBarStyle: UIStatusBarStyle {
            return .lightContent
        }
    }

其他回答

这里有十亿个答案,所以我想为什么不以扩展的形式添加另一个(在@ ceure的帮助下)

斯威夫特3

扩展:

extension UIApplication {
    class var statusBarBackgroundColor: UIColor? {
        get {
            return (shared.value(forKey: "statusBar") as? UIView)?.backgroundColor
        } set {
            (shared.value(forKey: "statusBar") as? UIView)?.backgroundColor = newValue
        }
    }
}

实现:

UIApplication.statusBarBackgroundColor = .blue

警告


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

这对我很有用

适用于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)

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
}

在阅读了所有的建议,并尝试了一些事情之后,我可以使用以下步骤让它为特定的视图控制器工作:

第一步:

打开你的信息。并插入一个名为“View controller based status bar appearance”的新键到NO

第二步(只是一个解释,不需要实现这个):

通常我们在应用程序中放入以下代码(_:didFinishLaunchingWithOptions:) 方法,

斯威夫特2

UIApplication.sharedApplication().statusBarStyle = .LightContent

斯威夫特3

UIApplication.shared.statusBarStyle = .lightContent

但那会影响所有viewcontroller的statusBarStyle。

那么,如何让它为特定的viewcontroller工作-最后一步:

打开你想要更改statusBarStyle的视图控制器文件,并将以下代码放在viewWillAppear()中,

斯威夫特2

UIApplication.sharedApplication().statusBarStyle = .LightContent

斯威夫特3

UIApplication.shared.statusBarStyle = .lightContent

同样,为特定的viewController实现viewWillDisappear()方法,并放入以下代码行:

斯威夫特2

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default

}

斯威夫特3

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
}

这一步将首先更改特定视图控制器的statusBarStyle,然后在特定视图控制器消失时将其更改回默认值。不实现viewWillDisappear()将永久性地将statusBarStyle更改为UIStatusBarStyle的新定义值。LightContent