我正在尝试隐藏我的一个视图控制器的状态栏(当模态显示时)。当我呈现视图控制器时,状态栏将被隐藏,然后在关闭时返回。
我已将以下代码添加到呈现的视图控制器中
- (BOOL)prefersStatusBarHidden
{
return YES;
}
我还将 Info.plist 文件中的键设置为以下内容:
<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>
据我了解,这应该是完成这项工作所需的全部内容。
我还使用自定义动画控制器来进行符合UIViewControllerAnimatedTransitioning
协议的呈现。在animateTransition:
实现中,我尝试手动调用prefersStatusBarHidden
,然后setNeedsStatusBarAppearanceUpdate
确保正在调用,但状态栏仍然存在。
任何想法为什么会发生这种情况将不胜感激。我已经搜索过 StackOverflow,但似乎没有人遇到过这个问题,所有接受的答案都是关于调用setNeedsStatusBarAppearanceUpdate
的,我已经在这样做了。
编辑-下面的代码现在似乎可以按需要工作
- (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;
}