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

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


当前回答

(截至2021年10月25日)

Swift 5, Swift 4.2, Swift 4

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
    .lightContent
}

其他回答

我在这个问题上遇到了一些麻烦。我不太喜欢在视图中全局改变状态栏的颜色,然后在视图中改变它,然后像接受的答案一样消失。信不信由你,你可以通过在你想要的视图控制器上重写preferredStatusBarStyle来实现。经过很长一段时间,这是我所做的让它工作:

Change View controller-based status bar appearance in your info.plist to YES. Now any full screen view controller can change the status bar style by overriding preferredStatusBarStyle. I specify full screen because this will not work for (non-full screen) modal view controllers, not without setting modal​Presentation​Captures​Status​Bar​Appearance to Yes that is. Also if you have embedded view controllers, like in a navigation controller for example, it will ask the top most view controller for status bar style. Overriding child​View​Controller​For​Status​Bar​Style and passing the embedded view controller is supposed to work but it didn't for me. So I just returned the embedded view controllers preferred status bar as the preferred status bar style. Something like this: override var preferredStatusBarStyle: UIStatusBarStyle { if let topViewController = viewControllers.last { return topViewController.preferredStatusBarStyle } return .default }

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

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

我在Swift 5、Swift 4.2中使用这种方式。

在Info.plist中添加下一个值:

UIViewControllerBasedStatusBarAppearance = YES

or

UIViewControllerBasedStatusBarAppearance = NO(查看变化)

UIStatusBarHidden = NO

UIStatusBarStyle = UIStatusBarStyleDefault(或设置为UIStatusBarStyleLightContent,如果你想在启动时看到轻状态栏文本)

然后将代码放置到您想要看到浅色内容的特定视图控制器(要看到深色文本,请将preferredStatusBarStyle设置为. darkcontent)。

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

override func viewDidLoad() {
    super.viewDidLoad()

    if let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as? UIView {
        statusBar.backgroundColor = .sunflowerYellow
    }
}

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

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

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

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

class YourViewController: UIViewControllerWithLightStatusBar {
...
}

这就是全部……