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

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


当前回答

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

其他回答

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

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

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

在Swift 5或xcode 11及以后的版本中设置(查看基于控制器的状态栏外观)key In info。plist为NO 然后转到项目目标,选择通用,设置状态栏样式为深色或浅色

这里有十亿个答案,所以我想为什么不以扩展的形式添加另一个(在@ 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

有两种情况:

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
}

自定义状态栏颜色(iOS11+, Swift4+)

如果您正在寻找如何将状态栏更改为自定义颜色的解决方案,这是可行的解决方案。

let statusBarView = UIView()
view.addSubview(statusBarView)
statusBarView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
    statusBarView.topAnchor.constraint(equalTo: view.topAnchor),
    statusBarView.leftAnchor.constraint(equalTo: view.leftAnchor),
    statusBarView.rightAnchor.constraint(equalTo: view.rightAnchor),
    statusBarView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
])
statusBarView.backgroundColor = .blue