ios IOS7 状态栏隐藏/显示在选择的控制器上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18915092/
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
IOS7 Status bar hide/show on select controllers
提问by StuartM
I would like to show and hide the Status bar on some controllers. Can this be done or is it more of an overall app setting.
我想在某些控制器上显示和隐藏状态栏。这可以完成还是更像是一个整体的应用程序设置。
I have seen many posts/questions about the plist update:
我看过很多关于 plist 更新的帖子/问题:
View controller-based status bar appearance - NO
If this is completed what control is then given?
如果这完成了,那么给予什么控制?
I am looking to show the status bar on the main screen of the application. But for example on a side (slide) menu I would like it not to show, is this possible? Can this be changed in IB or code?
我希望在应用程序的主屏幕上显示状态栏。但是例如在侧面(幻灯片)菜单上我不希望它显示,这可能吗?这可以在 IB 或代码中更改吗?
EDIT -- I am using a https://github.com/edgecase/ECSlidingViewControllerimplementation.
编辑 - 我正在使用https://github.com/edgecase/ECSlidingViewController实现。
The main controller (showing the first page) should show the Status bar, but the left menu controller when it slides should not.
主控制器(显示第一页)应显示状态栏,但滑动时左侧菜单控制器不应显示。
I believe the issue is that they both sit within the same root controller (sliding view controller) so it is difficult to complete.
我认为问题在于它们都位于同一个根控制器(滑动视图控制器)中,因此很难完成。
Ideally if the home screen (main page) could take the status bar with it when it slides that would look best.
理想情况下,如果主屏幕(主页面)可以在滑动时显示状态栏,那将是最好的状态。
回答by auco
The plist setting "View controller-based status bar appearance"
only controls if a per-controller based setting should be applied on iOS 7.
plist 设置"View controller-based status bar appearance"
仅控制是否应在 iOS 7 上应用基于每个控制器的设置。
If you set this plist option to NO, you have to manually enable and disable the status bar like (as it was until iOS 6):
如果将此 plist 选项设置为 NO,则必须手动启用和禁用状态栏,例如(直到 iOS 6):
[[UIApplication sharedApplication] setStatusBarHidden:YES]
If you set this plist option to YES, you can add this method to each of your viewControllers to set the statusBar independently for each controller (which is esp. nice if you have a smart subclass system of viewControllers)
如果将此 plist 选项设置为 YES,则可以将此方法添加到每个 viewController 中,以便为每个控制器独立设置 statusBar(尤其是如果您有一个智能的 viewController 子类系统,那就太好了)
- (BOOL)prefersStatusBarHidden {
return YES;
}
Edit:
编辑:
there are two more methods that are of interest if you are opting in the new viewController-based status bar appearance -
如果您选择新的基于 viewController 的状态栏外观,还有两种方法值得关注 -
Force a statusbar update with:
强制更新状态栏:
[self setNeedsStatusBarAppearanceUpdate]
If you have nested controllers (e.g. a contentViewController in a TabBarController subclass, your TabBarController subclass might ask it's current childViewController and forward this setting. I think in your specific case that might be of use:
如果您有嵌套控制器(例如 TabBarController 子类中的 contentViewController,您的 TabBarController 子类可能会询问它是当前的 childViewController 并转发此设置。我认为在您的特定情况下可能有用:
- (UIViewController *)childViewControllerForStatusBarHidden {
return _myChildViewController;
}
- (UIViewController *)childViewControllerForStatusBarStyle {
return _myOtherViewController;
}
回答by Mojo66
On iOS 7 and later, just implement -prefersStatusBarHidden
, for example in a UIViewController
that should hide the status bar:
在 iOS 7 及更高版本上,只需实现-prefersStatusBarHidden
,例如在UIViewController
应该隐藏状态栏的 a 中:
- (BOOL)prefersStatusBarHidden {
return YES;
}
The default is NO
.
默认值为NO
.
回答by Danut Pralea
Swift 3:
斯威夫特 3:
override var prefersStatusBarHidden: Bool {
return true
}
回答by Natalia
You can also show/hide the status bar in an animation block, by putting animation code inside didSet property of variable that describes whether it should be shown or hidden. When you set a new value for the statusBarHidden
Bool, this automatically triggers the animated updating of the status bar over the duration you have chosen.
您还可以在动画块中显示/隐藏状态栏,方法是将动画代码放在变量的 didSet 属性中,该属性描述了它是应该显示还是隐藏。当您为statusBarHidden
Bool设置新值时,这会在您选择的持续时间内自动触发状态栏的动画更新。
/// Swift 3 syntax:
var statusBarHidden: Bool = true {
didSet {
UIView.animate(withDuration: 0.5) { () -> Void in
self.setNeedsStatusBarAppearanceUpdate()
}
}
}
override var prefersStatusBarHidden: Bool {
return statusBarHidden
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
statusBarHidden = false // show statusBar, animated, by triggering didSet block
}