如何更改状态栏样式 - iOS 12

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52238121/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:47:07  来源:igfitidea点击:

How to change status bar style - iOS 12

iosswiftios12

提问by Tomá? Pánik

I need to update status bar style on every view controller based on the background color (what UINavigationControlleris doing automatically).

我需要根据背景颜色(UINavigationController自动执行的操作)更新每个视图控制器上的状态栏样式。

Have tried all the options described on stackoverflow (View controller-based status bar appearancein info.plistset to YES), but none worked for me.

试过在计算器(描述的所有选项View controller-based status bar appearanceinfo.plist设置为YES),但没有为我工作。

I am using Xcode 10 beta 6 and Swift 4.2, targeting iOS 12.

我正在使用 Xcode 10 beta 6 和 Swift 4.2,针对 iOS 12。

回答by ielyamani

Set View controller-based status bar appearanceto NOin the info.plistand override preferredStatusBarStylein each view controller like so:

设置View controller-based status bar appearanceNOinfo.plist和覆盖preferredStatusBarStyle在像这样每个视图控制器:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

And call setNeedsStatusBarAppearanceUpdate()in your view controller (in viewDidLoad()for example).

并调用setNeedsStatusBarAppearanceUpdate()您的视图控制器(viewDidLoad()例如)。

回答by bsod

Swift 4.2, iOS 12

斯威夫特 4.2,iOS 12

View controller-based status bar appearancenow needs to be set to YESin info.plistsince UIKit no longer wants us to edit status bar style through UIApplication.shared--status bar style is now view-controller based.

View controller-based status bar appearancenow 需要设置为YESininfo.plist因为 UIKit 不再希望我们通过UIApplication.shared--status bar style 现在是基于视图控制器来编辑状态栏样式。

Then if you want the change to be applied at app level, simply override preferredStatusBarStylein the appropriate container view controller (ideally the root)...

然后,如果您希望在应用程序级别应用更改,只需preferredStatusBarStyle在适当的容器视图控制器(最好是根)中覆盖...

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

...and this will propagate to all view controllers. And if you want to edit status bar style per view controller, apply this override per view controller.

...这将传播到所有视图控制器。如果您想编辑每个视图控制器的状态栏样式,请为每个视图控制器应用此覆盖。

If status bar style ever changes during runtime, then you need to call setNeedsStatusBarAppearanceUpdate()(from anywhere in the container/root view controller or that specific view controller), otherwise it isn't needed.

如果状态栏样式在运行时发生变化,则您需要调用setNeedsStatusBarAppearanceUpdate()(从容器/根视图控制器或该特定视图控制器中的任何位置),否则不需要。

回答by Jim Bray

I have Xcode 10.2 and found as you did that setting the View controller-based status bar appearance in info.plist to "YES" will not change the status bar style at all.

However, I did find that changing two keys in the info.plist file will do all the work of changing the status bar to light or dark without any additional coding.

Here's what I did to fix it for myself
When hovering over the top line "Information Property List" from the info.plist file, a small round "+" button will appear. Click this button and scroll through the items until you find the following keys.

View controller-based status bar appearance[set this value to] NO
Status bar style [set this value to] UIStatusBarStyleLightContent

NOTE:The UIStatusBarStyleLightContent is not found as a selectable item in the value list and must be typed into the value box.

info.plist

我有 Xcode 10.2,发现在 info.plist 中将基于视图控制器的状态栏外观设置为“是”根本不会改变状态栏样式。

但是,我确实发现更改 info.plist 文件中的两个键将完成将状态栏更改为亮或暗的所有工作,而无需任何额外的编码。

这是我为自己修复它所做的
当将鼠标悬停在 info.plist 文件的第一行“信息属性列表”上时,会出现一个小的圆形“+”按钮。单击此按钮并滚动项目,直到找到以下键。




UIStatusBarStyleLightContent 在值列表中找不到作为可选项目,必须输入到值框中。

信息.plist

I hope this helps you or anyone else looking for an answer to this question.

我希望这可以帮助您或其他任何人寻找这个问题的答案。

回答by swearwolf

If you have View controller-based status bar appearancein info.plistset to YESand your view controller is embedded into UINavigationController, then your navigation controller will be responsible for updating bar style (through navigationController.navigationBar.barStyle) and preferredStatusBarStyleproperty will be ignored

如果您将View controller-based status bar appearanceininfo.plist设置为YES并且您的视图控制器嵌入到 中UINavigationController,那么您的导航控制器将负责更新栏样式(通过navigationController.navigationBar.barStyle)并且preferredStatusBarStyle属性将被忽略

回答by thexande

If you're wrapped in a nav controller, you're going to need this:

如果你被包裹在一个导航控制器中,你将需要这个:

final class LightNavigationController: UINavigationController {
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
}