我正在try 隐藏其中一个视图控制器的状态栏(以模式显示时).当我展示视图控制器时,状态栏将被隐藏,然后在关闭时返回.
我已经向显示的视图控制器添加了以下代码
- (BOOL)prefersStatusBarHidden
{
return YES;
}
我还设置了信息中的键.plist文件到以下位置:
<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>
据我所知,这应该是所有需要使这项工作.
我还使用一个自定义的动画控制器来做演示,这符合UIViewControllerAnimatedTransitioning
协议.在animateTransition:
实现中,我try 手动调用prefersStatusBarHidden
,然后是setNeedsStatusBarAppearanceUpdate
,以确保调用正在进行,但状态栏仍然存在.
如果您有任何关于为什么会发生这种情况的 idea ,我们将不胜感激.我已经搜索了StackOverflow,但似乎没有人遇到过这个问题,所有被接受的答案都指向拨打setNeedsStatusBarAppearanceUpdate
,我已经在这么做了.
EDIT-下面的代码现在似乎符合要求
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
if (self.isPresenting) {
UIView *containerView = [transitionContext containerView];
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
toViewController.view.frame = containerView.frame;
[containerView addSubview:toViewController.view];
// Ask the presented controller whether to display the status bar
[toViewController setNeedsStatusBarAppearanceUpdate];
[UIView animateWithDuration:1.0f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
toViewController.view.alpha = 1.0f;
fromViewController.view.alpha = 0.0f;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
else {
// do the reverse
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
[UIView animateWithDuration:1.0f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
toViewController.view.alpha = 1.0f;
fromViewController.view.alpha = 0.0f;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
// Once dismissed - ask the presenting controller if the status bar should be presented
[toViewController setNeedsStatusBarAppearanceUpdate];
}];
}
}
....
// PresentingController.m
- (BOOL)prefersStatusBarHidden
{
if (self.presentedViewController) {
return YES;
}
return NO;
}
// PresentedController.m
- (BOOL)prefersStatusBarHidden
{
return YES;
}