ios 无法隐藏状态栏——Swift 3,

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

Can't Hide Status Bar—Swift 3,

iosswiftxcodeswift3

提问by owlswipe

I usually hide the status bar with

我通常隐藏状态栏

override func prefersStatusBarHidden() -> Bool {
    return true
}

but Xcode is giving me an error, saying "Method does not override anything from its superclass".

但是 Xcode 给了我一个错误,说“方法不会覆盖其超类中的任何内容”。

If I delete the override, Xcode gives a different error: "Method 'prefersStatusBarHidden()' with Objective-C selector 'prefersStatusBarHidden' conflicts with getter for 'prefersStatusBarHidden' from superclass 'UIViewController' with the same Objective-C selector"

如果我删除override,Xcode 会给出一个不同的错误:“方法 'prefersStatusBarHidden()' 与 Objective-C 选择器 'prefersStatusBarHidden' 与来自超类 'UIViewController' 的 getter 冲突,使用相同的 Objective-C 选择器”



I also have "Hide Status Bar" checked in my Target's general settings:

我还在目标的常规设置中选中了“隐藏状态栏”:

enter image description here

在此处输入图片说明

but the status bar still shows up.

但状态栏仍然显示。



I found this method in another Stack Overflow answer

我在另一个 Stack Overflow 答案中找到了这个方法

UIApplication.shared.setStatusBarHidden(true, with: .none)

but that doesn't hide the status bar either.

但这也不会隐藏状态栏。



In Xcode 8 Beta 1, I used the first and second methods, which worked to hide the status bar (the first method did not return an error). What can I do now to hide the status bar, with Xcode 8 Beta 4?

在 Xcode 8 Beta 1 中,我使用了第一种和第二种方法,它们可以隐藏状态栏(第一种方法没有返回错误)。我现在可以做些什么来隐藏状态栏,使用 Xcode 8 Beta 4?

Note: The status bar shows up on Simulator devices and physical devices, all running iOS 10.

注意:状态栏显示在模拟器设备和物理设备上,所有设备都运行 iOS 10。

回答by Anbu.Karthik

We need to override the property itself on Swift 3 (this is new in Xcode 8 Beta 4):

我们需要在 Swift 3 上覆盖属性本身(这是 Xcode 8 Beta 4 中的新功能):

override var prefersStatusBarHidden: Bool {  
    return true  
}  


updated Swift 5+

更新 Swift 5+

override var prefersStatusBarHidden: Bool { true }

for another example also you can get hereand here

再举一个例子,你也可以在这里这里

For more on what this change is and why it's necessary, see Matt's great answer on this.

有关此更改是什么以及为什么有必要的更多信息,请参阅Matt 对此的出色回答