ios UIStatusBarStyle 在 Swift 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24235401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIStatusBarStyle not working in Swift
提问by davidrayowens
I'm trying to change the Status Bar color in my Swift app to white, but am hitting a brick wall. I have 3 ViewControllers that are each embedded in a NavigationController (could that be the issue? I've already tried to place the code in the NavigationController class.) I've tried both of the following pieces of code in the didFinishLaunchingWithOptions of my AppDelegate.swift file but neither worked.
我正在尝试将 Swift 应用程序中的状态栏颜色更改为白色,但遇到了砖墙。我有 3 个 ViewControllers,每个都嵌入在 NavigationController 中(这可能是问题吗?我已经尝试将代码放在 NavigationController 类中。)我已经在我的 AppDelegate 的 didFinishLaunchingWithOptions 中尝试了以下两段代码.swift 文件,但都没有工作。
application.statusBarStyle = .LightContent
and
和
UIApplication.sharedApplication().statusBarStyle = .LightContent
All that the Docs have to say about it is that UIBarButtonStyle is an Int and gave me this enum snippet which didn't help me at all with implimentation.
文档要说的就是 UIBarButtonStyle 是一个 Int 并给了我这个枚举片段,它对我的实现没有任何帮助。
enum UIStatusBarStyle : Int {
case Default
case LightContent
case BlackOpaque
}
What am I missing?
我错过了什么?
回答by Mick MacCallum
You have two options.
你有两个选择。
If you want to continue manually setting the style of the status bar, continue doing what you're doing, but you'll need to add the following key to your info.plist file with a value of NO
.
如果您想继续手动设置状态栏的样式,请继续执行您正在执行的操作,但是您需要将以下键添加到您的 info.plist 文件中,其值为NO
。
View controller-based status bar appearance
查看基于控制器的状态栏外观
Or, if you want to continue to use view controller based status bar appearance, instead of setting the application's statusBarStyle, override the preferredStatusBarStyle
property in each view controller for which you'd like to specify a status bar style.
或者,如果您想继续使用基于视图控制器的状态栏外观,而不是设置应用程序的 statusBarStyle,请覆盖preferredStatusBarStyle
您要为其指定状态栏样式的每个视图控制器中的属性。
Swift 3
斯威夫特 3
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Swift 2
斯威夫特 2
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
回答by Maselko
Swift 3.0
斯威夫特 3.0
in AppDelegate.swift didFinishLaunchingWithOptions
在 AppDelegate.swift didFinishLaunchingWithOptions
UIApplication.shared.statusBarStyle = .lightContent
Info.plist
信息表
View controller-based status bar appearance -> NO
Swift 2.2
斯威夫特 2.2
in AppDelegate.swift didFinishLaunchingWithOptions
在 AppDelegate.swift didFinishLaunchingWithOptions
UIApplication.sharedApplication().statusBarStyle = .LightContent
Info.plist
信息表
View controller-based status bar appearance -> NO
回答by Nico S.
You have to set the:
你必须设置:
navigationController.navigationBar.barStyle = .black
navigationController.navigationBar.barStyle = .black
and the text will appear in white
文本将显示为白色
回答by Biodave
For iOS9.x and Xcode7, just put this inside AppDelegate.swift
:
对于 iOS9.x 和 Xcode7,只需将其放入AppDelegate.swift
:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UINavigationBar.appearance().barStyle = .Black
}
This will automatically turn your status bar's style to .Lightcontent
for all the view controllers inside a UINavigationController.
这将自动将状态栏的样式转换.Lightcontent
为 UINavigationController 中的所有视图控制器。
(Also, delete View controller-based status bar appearance
from Info.plist to suppress the warnings you're probably seeing too!)
(另外,View controller-based status bar appearance
从 Info.plist 中删除以抑制您可能看到的警告!)
回答by Eric Welander
In Swift 3, status bar style has changed to a computed property in UIViewController that you can override like this:
在 Swift 3 中,状态栏样式已更改为 UIViewController 中的计算属性,您可以像这样覆盖它:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent //or default
}
回答by GuiSoySauce
On iOS 9 the following (setStatusBarStyle) is deprecated and you will get a warning if you go that way.
在 iOS 9 上,以下 (setStatusBarStyle) 已被弃用,如果您这样做,您将收到警告。
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
If you want all statusBars changed in a single shot try adding the following to your Info.plist. This will also make your launch-screen status bar white. While the code above won't.
如果您希望一次性更改所有状态栏,请尝试将以下内容添加到您的 Info.plist。这也将使您的启动屏幕状态栏变白。虽然上面的代码不会。
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleLightContent</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
回答by Constantin Saulenco
for me all above dind't work until i add:
对我来说,上述所有内容都不起作用,直到我添加:
self.navigationController?.navigationBar.barStyle = .black;
so:
所以:
- Set
UIViewControllerBasedStatusBarAppearance
toYES
in.plist
- In
viewDidLoad
callself.setNeedsStatusBarAppearanceUpdate();
- Override preferredStatusBarStyle
override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent }
- In overrided method i set also the
navigationBar.barStyle
so final
for lightcontent:override var preferredStatusBarStyle: UIStatusBarStyle { self.navigationController?.navigationBar.barStyle = .black;//or default return .lightContent //or default }
and for blackcontent use default
- 设置
UIViewControllerBasedStatusBarAppearance
于YES
在.plist
- 在
viewDidLoad
通话self.setNeedsStatusBarAppearanceUpdate();
- 覆盖 preferredStatusBarStyle
override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent }
- 在覆盖方法中,我还为浅色内容设置了
navigationBar.barStyle
so final :对于黑色内容使用默认值override var preferredStatusBarStyle: UIStatusBarStyle { self.navigationController?.navigationBar.barStyle = .black;//or default return .lightContent //or default }
and if this doesn't work you can try add a UINavigationController
extension
:
如果这不起作用,您可以尝试添加UINavigationController
extension
:
extension UINavigationController
{
override open var preferredStatusBarStyle: UIStatusBarStyle {
if let lastVC = self.viewControllers.last
{
return lastVC.preferredStatusBarStyle
}
return .default
}
}
回答by Lord iPhonius
Strange, using Swift 3.1 & XC8.2.1, but all of the above didn't work.
奇怪,使用 Swift 3.1 和 XC8.2.1,但以上所有方法都不起作用。
What I did, is just
我所做的,只是
extension UINavigationController
{
override open var preferredStatusBarStyle: UIStatusBarStyle {
get {
return .lightContent
}
}
}
No Plist, no other stuff. HTH
没有Plist,没有其他东西。HTH
回答by Denis Kreshikhin
In Swift 3.0 you can override a getter in ViewController for View controller-based status bar appearance:
在 Swift 3.0 中,您可以覆盖 ViewController 中的 getter 以获得基于视图控制器的状态栏外观:
override var preferredStatusBarStyle: UIStatusBarStyle {
get { return .lightContent }
}
回答by Igor
Step 1. Add to info.plist View controller-based status bar appearance -> NO
步骤 1. 添加到 info.plist View controller-based status bar appearance -> NO
Step 2. Add code in method where you need to change status bar color:
步骤 2. 在需要更改状态栏颜色的方法中添加代码:
UIApplication.shared.statusBarStyle = .lightContent //(or .default)
setNeedsStatusBarAppearanceUpdate()
Key line of code: setNeedsStatusBarAppearanceUpdate()
关键代码行: setNeedsStatusBarAppearanceUpdate()