如何在 iOS 9 中隐藏单个视图控制器的状态栏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35028262/
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
How to hide status bar of a single view controller in iOS 9?
提问by TIMEX
In a ViewController, which I presented modally, I did this:
在我以模态方式呈现的 ViewController 中,我这样做了:
override func prefersStatusBarHidden() -> Bool {
return true
}
This used to work, but it no longer works. What's the best way to hide the status bar onlyfor this view controller?
这曾经有效,但现在不再有效。仅为此视图控制器隐藏状态栏的最佳方法是什么?
回答by Pochi
For Swift 3& Swift 4it has changed to overriding a variable like this:
对于Swift 3和Swift 4,它已更改为覆盖这样的变量:
override var prefersStatusBarHidden: Bool {
return true
}
If you want to "Update" the state once the view controller is already being displayed, you will need to call:
如果要在视图控制器已经显示后“更新”状态,则需要调用:
setNeedsStatusBarAppearanceUpdate()
Please refer to the documentation.
请参阅文档。
回答by gandhi Mena
For Swift 3 and Swift 4.2when view going to Appear
对于Swift 3 和 Swift 4.2,当视图出现时
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
UIApplication.shared.isStatusBarHidden = true
}
when view goint to Dissapear
当视图消失时
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.shared.isStatusBarHidden = false
}
It's possible you need set in your info.plist, next line:
您可能需要在 info.plist 中设置下一行:
View controller-based status bar appearance = NO
回答by anas.p
In your UIViewController:
在你的 UIViewController 中:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIApplication.shared.isStatusBarHidden = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
//It will show the status bar again after dismiss
UIApplication.shared.isStatusBarHidden = false
}
override var prefersStatusBarHidden: Bool {
return true
}
回答by Greg
In iOS 9, Xcode 7, Swift 2.0, it's back to how it was previously.
在 iOS 9、Xcode 7、Swift 2.0 中,它又回到了以前的样子。
override func prefersStatusBarHidden() -> Bool {
return true
}
In fact Xcode will tell you that
事实上 Xcode 会告诉你
UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: .None)
has been deprecated and that you should use the prefersStatusBarHidden method.
已被弃用,您应该使用 prefersStatusBarHidden 方法。
回答by Ali BOZO?LU
For Swift 3,
对于 Swift 3,
override var prefersStatusBarHidden: Bool{
return true
}
and add viewDidLoad()
并添加 viewDidLoad()
self.modalPresentationCapturesStatusBarAppearance = true
回答by Jayprakash Dubey
回答by Rizwan Mehboob
Hide Status Bar smoothly by just using UIAnimation and stored property.
只需使用 UIAnimation 和存储属性即可顺利隐藏状态栏。
Swift 3+
斯威夫特 3+
var statusBarState = false
override var prefersStatusBarHidden: Bool{
return statusBarState
}
And then in viewWillAppear
然后在 viewWillAppear
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
statusBarState = true
UIView.animate(withDuration: 0.30) {
self.setNeedsStatusBarAppearanceUpdate()
}
}
回答by Johnny Rockex
For those still struggling, the below works for iOS9.
对于那些仍在苦苦挣扎的人,以下适用于 iOS9。
You update the rootViewController prefersStatusBarHidden function by calling it from your individual child/grandchild viewControllers. This works where you add childViewControllers directly to your rootViewController.
您可以通过从您的单个子/孙子视图控制器调用它来更新 rootViewController prefersStatusBarHidden 函数。这适用于您将 childViewControllers 直接添加到您的 rootViewController。
You don't need to set anything in the info.plist, but setting setting 'statusBarIsInitiallyHidden' works independently of the below.
您不需要在 info.plist 中设置任何内容,但设置 'statusBarIsInitiallyHidden' 的设置独立于以下内容。
First, in your rootViewController, add the following:
首先,在您的 rootViewController 中,添加以下内容:
-(void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateStatusBarAppearance:) name:@"kStatusBarAppearance" object:nil];
}
-(void)updateStatusBarAppearance:(NSNotification *)n {
statusBarIsHidden = [n.object boolValue];
[self setNeedsStatusBarAppearanceUpdate];
}
-(UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent; //optional
}
-(BOOL)prefersStatusBarHidden{
return statusBarIsHidden;
}
Then, in the single view controller where you want to hide the status bar, call this:
然后,在要隐藏状态栏的单个视图控制器中,调用:
-(void)viewDidLoad {
[[NSNotificationCenter defaultCenter] postNotificationName:@"kStatusBarAppearance" object:[NSNumber numberWithBool:true]];
}
-(void)popSelf {
[[NSNotificationCenter defaultCenter] postNotificationName:@"kStatusBarAppearance" object:[NSNumber numberWithBool:false]];
}
回答by Hakan Baybas
A late answer but if you need an alternative solution you can use this:
一个迟到的答案,但如果你需要一个替代解决方案,你可以使用这个:
public func ShowStatusBar() {
let statusBarWindow = UIApplication.shared.value(forKey: "statusBarWindow") as? UIWindow
UIView.animate(withDuration: 0.3) {
statusBarWindow?.alpha = 1
}
}
public func HideStatusBar() {
let statusBarWindow = UIApplication.shared.value(forKey: "statusBarWindow") as? UIWindow
UIView.animate(withDuration: 0.3) {
statusBarWindow?.alpha = 0
}
}
回答by eonist
When all else fails (as it did for me) Swift 4.1
当所有其他方法都失败时(就像对我一样)Swift 4.1
No editing of .plist required. And AppStore will approve it
不需要编辑 .plist。AppStore 会批准它
(UIApplication.shared.value(forKey: "statusBarWindow") as? UIWindow)?.isHidden = false