如何在 Swift 3 中设置状态栏样式 [英] How to set Status Bar Style in Swift 3

查看:27
本文介绍了如何在 Swift 3 中设置状态栏样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Xcode 8.0 beta 4.

I'm using Xcode 8.0 beta 4.

在之前的版本中,UIViewController 有设置状态栏样式的方法

In previous version, UIViewController have method to set the status bar style

public func preferredStatusBarStyle() -> UIStatusBarStyle

但是,我发现它在 Swift 3 中更改为仅获取变量".

However, I found it changed to a "Get ONLY varaiable" in Swift 3.

public var preferredStatusBarStyle: UIStatusBarStyle { get } 

如何提供要在我的 UIViewController 中使用的样式?

How can provide the style to use in my UIViewController?

推荐答案

[UPDATED] For Xcode 10+ &斯威夫特 4.2+

这是 iOS 7 及更高版本的首选方法

在您的应用程序的Info.plist 中,将View controller-based status bar appearance 设置为YES.

In your application's Info.plist, set View controller-based status bar appearance to YES.

覆盖 preferredStatusBarStyle(Apple 文档) 在每个视图控制器中.例如:

Override preferredStatusBarStyle (Apple docs) in each of your view controllers. For example:

override var preferredStatusBarStyle: UIStatusBarStyle {     
      return .lightContent
}

如果您让 preferredStatusBarStyle 根据视图控制器内部的变化(例如,滚动位置或显示的图像是否变暗)返回不同的首选状态栏样式,那么您当该状态发生变化时,将需要调用 setNeedsStatusBarAppearanceUpdate().

If you have preferredStatusBarStyle returning a different preferred status bar style based on something that changes inside of your view controller (for example, whether the scroll position or whether a displayed image is dark), then you will want to call setNeedsStatusBarAppearanceUpdate() when that state changes.

iOS 7 之前的版本,已弃用的方法

Apple 已弃用此功能,因此它将在未来.使用上面的方法,这样下一个iOS版本发布时就不用重写了.

Apple has deprecated this, so it will be removed in the future. Use the above method so that you don't have to rewrite it when the next iOS version is released.

如果您的应用程序将支持在您的应用程序的Info.plist 中,将View controller-based status bar appearance 设置为NO.

If your application will support In your application's Info.plist, set View controller-based status bar appearance to NO.

appDelegate.swiftdidFinishLaunchingWithOptions函数中,添加:

UIApplication.shared.statusBarStyle = .lightContent

对于导航控制器

如果您使用导航控制器,并且希望使用每个视图控制器的首选状态栏样式,请将 基于视图控制器的状态栏外观 设置为 YES您的应用程序的 info.plist

If you use a navigation controller and you want the preferred status bar style of each view controller to be used and set View controller-based status bar appearance to YES in your application's info.plist

extension UINavigationController {
   open override var preferredStatusBarStyle: UIStatusBarStyle {
      return topViewController?.preferredStatusBarStyle ?? .default
   }
}

这篇关于如何在 Swift 3 中设置状态栏样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆