问题描述
我有一个标准的Master-Detail应用程序,我正试图有条件地显示/隐藏状态栏。
I have a standard Master-Detail Application, and I'm trying to conditionally show/hide the status bar.
覆盖 prefersStatusBarHidden( )MasterViewController中的
什么都不做。它甚至都没有被调用。
Overriding prefersStatusBarHidden()
in MasterViewController does nothing. It never even gets called.
override func prefersStatusBarHidden() -> Bool {
return true
}
设置 UIViewControllerBasedStatusBarAppearance Info.plist中的
没有帮助,大概是因为 YES
已经是默认值。调用 setNeedsStatusBarAppearanceUpdate()
也无济于事。
Setting UIViewControllerBasedStatusBarAppearance
in Info.plist doesn't help, presumably since YES
is already the default value. Calling setNeedsStatusBarAppearanceUpdate()
doesn't help either.
我的目标是iOS 9。
I am targeting iOS 9.
推荐答案
有一点清洁解决方案。有一个函数 childViewControllerForStatusBarHidden
,它专门用于返回一个子视图控制器,prefersStatusBarHidden应转发给它。
There is a little bit cleaner solution. There is a function childViewControllerForStatusBarHidden
which is specifically designed to return a child view controller to which prefersStatusBarHidden should be forwarded.
因此,覆盖它会更好。它看起来像这样:
So, it will be better to override it. It will look like this:
override func childViewControllerForStatusBarHidden() -> UIViewController? {
if var topViewController = self.viewControllers.first {
if let navigationController = topViewController as? UINavigationController {
topViewController = navigationController.topViewController!
}
return topViewController
}
return super.childViewControllerForStatusBarHidden()
}
可能你甚至可以省略以下内容。 NavigationViewController有自己的childViewControllerForStatusBarHidden(),它会将它发送到子视图控制器。
And probably you can even omit following. NavigationViewController has childViewControllerForStatusBarHidden() on it's own which will send it to child viewcontroller.
if let navigationController = topViewController as? UINavigationController {
topViewController = navigationController.topViewController!
}
这篇关于prefersStatusBarHidden没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!