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

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


当前回答

自定义状态栏颜色(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

其他回答

实现preferredStatusBarStyle,如你所述,并调用self.setNeedsStatusBarAppearanceUpdate()在ViewDidLoad和 也在信息。设置UIViewControllerBasedStatusBarAppearance为YES(默认是YES)

目前还不清楚为什么它不起作用。我需要检查代码。另一个建议是 使用viewDidLoad UIApplication.sharedApplication()中的工作代码。statusBarStyle = . lightcontent,并将此更改为默认值,当您查看viewgetdisappear viewWillDisappear。

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

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

截至2020年10月,Swift 5, Xcode 12

如果你想把它设置为应用中的所有视图控制器如果你的应用有导航控制器。

你可以在plist文件中这样做,如下所示:

对我有用的是,在故事板中,转到导航控制器,选择导航栏,单击属性检查器,然后将默认样式改为黑色。就是这样!

对于swift 3

.plist

View controller-based status bar appearance = NO

AppDelegate.swift

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Custom statubar
        UIApplication.shared.isStatusBarHidden = false
        UIApplication.shared.statusBarStyle = .lightContent
        let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
        statusBar.backgroundColor = UIColor.gray

        return true
    }