ios 隐藏状态栏 swift 4
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46543470/
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
hide status bar swift 4
提问by Dragisa Dragisic
I am trying to hide status bar in one of my UIViewControllers (Swift 4).
我试图在我的 UIViewControllers (Swift 4) 之一中隐藏状态栏。
Firstly, I set View controller-based status bar appearanceto YESin
Info.plist
.I overrode the
prefersStatusBarHidden
property in my controller:
首先,我设置视图控制器为基础的状态栏的外观到是在
Info.plist
。我覆盖了
prefersStatusBarHidden
控制器中的属性:
override var prefersStatusBarHidden: Bool {
return true
}
- And in
viewDidLoad()
, I addedsetNeedsStatusBarAppearanceUpdate()
function to force theprefersStatusBarHidden
property to be read.
- 在 中
viewDidLoad()
,我添加setNeedsStatusBarAppearanceUpdate()
了强制prefersStatusBarHidden
读取属性的函数。
After all that, I still see the status bar on that UIViewController
.
毕竟,我仍然看到状态栏UIViewController
。
Can someone help me, please?
有人能帮助我吗?
采纳答案by MachTurtle
You probably found your own solution to this already, but I got it working this way:
您可能已经找到了自己的解决方案,但我是这样解决的:
override func viewWillAppear(_ animated: Bool) {
// Sets the status bar to hidden when the view has finished appearing
let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
statusBar.isHidden = true
}
override func viewWillDisappear(_ animated: Bool) {
// Sets the status bar to visible when the view is about to disappear
let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
statusBar.isHidden = false
}
回答by Viraj Padsala
You can hide the status bar in any or all of your view controllers just by adding this code:
只需添加以下代码,您就可以在任何或所有视图控制器中隐藏状态栏:
override var prefersStatusBarHidden: Bool {
return true
}
Any view controller containing that code will hide the status bar by default.
默认情况下,任何包含该代码的视图控制器都会隐藏状态栏。
If you want to animate the status bar in or out, just call setNeedsStatusBarAppearanceUpdate()on your view controller – that will force prefersStatusBarHiddento be read again, at which point you can return a different value. If you want, your call to setNeedsStatusBarAppearanceUpdate()can actually be inside an animation block, which causes the status bar to hide or show in a smooth way.
如果您想为状态栏设置动画,只需在视图控制器上调用setNeedsStatusBarAppearanceUpdate()– 这将强制再次读取prefersStatusBarHidden,此时您可以返回不同的值。如果需要,您对setNeedsStatusBarAppearanceUpdate() 的调用实际上可以在动画块内,这会导致状态栏以平滑的方式隐藏或显示。
回答by Alok Sahay
If you are presenting the view controller modally, try
如果您以模态方式呈现视图控制器,请尝试
viewController.modalPresentationCapturesStatusBarAppearance = true
回答by Creeptosis
Although some implementations are cleaner such as:
虽然一些实现更干净,例如:
UIApplication.shared.isStatusBarHidden = true
UIApplication.shared.isStatusBarHidden = true
There are some weird clipping animations during transitions. Although more verbose, I prefer @MachTurtle's solution:
过渡期间有一些奇怪的剪辑动画。虽然更冗长,但我更喜欢@MachTurtle 的解决方案:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
if let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as? UIView{
statusBar.isHidden = true
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
statusBar.isHidden = false
}
Try it out, works great for me.
试试看,对我很有用。
回答by Halyna Rubashko
回答by nitish005
Use the following code
UIApplication.shared.isStatusBarHidden = true
使用以下代码
UIApplication.shared.isStatusBarHidden = true
this is the only thing that I found that is working in iOS11.
you can write in didFinishLaunchingWithOptions
or in 'viewWillAppear' of you BaseViewController
Enjoy.
这是我发现的唯一可以在 iOS11 中使用的东西。你可以写在 didFinishLaunchingWithOptions
或在'viewWillAppear'中你BaseViewController
喜欢。
回答by Ali Alaoi
Add this to your info.plist
将此添加到您的info.plist
<key>UIStatusBarHidden</key>
<true/>
回答by Leonif
When you try to overload statusbar properties for ViewController which in UINavigationStack - you need make extension below
当您尝试为 UINavigationStack 中的 ViewController 重载状态栏属性时 - 您需要在下面进行扩展
extension UINavigationController {
override open var childForStatusBarStyle: UIViewController? {
return self.topViewController
}
}
then your overloaded properties will became active
那么您的重载属性将变为活动状态
回答by Noman Haroon
I was searching for it and the one work for me is
我一直在寻找它,对我来说最重要的工作是
Swift 5
斯威夫特 5
override var prefersStatusBarHidden: Bool {
return true
}
回答by Shayeeb
As you said, you are using UINavigationController to navigate to your custom view controller. I suppose you have set your Custom View controller as the root view of your UINavigationController. In this case overriding var prefersStatusBarHidden in your custom view controller won't work but you will have to subclass your UINavigation Controller and override the property there as shown below:-
正如您所说,您正在使用 UINavigationController 导航到您的自定义视图控制器。我想您已经将自定义视图控制器设置为 UINavigationController 的根视图。在这种情况下,在您的自定义视图控制器中覆盖 var prefersStatusBarHidden 将不起作用,但您必须子类化 UINavigation 控制器并覆盖那里的属性,如下所示:-
class CustomNavigationController: UINavigationController {
override var prefersStatusBarHidden: Bool {
return true
}
}