我试图隐藏我的一个 View Controller 的状态栏(以模态显示时)。当我呈现 View Controller 时,状态栏将被隐藏,然后在关闭时返回。

我已经将以下代码添加到了提供的 View Controller 中

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

我还将Info.plist文件中的 key 设置为以下内容:
<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>

根据我的理解,这应该是完成此工作所需的全部。

我还使用自定义动画 Controller 进行符合UIViewControllerAnimatedTransitioning协议(protocol)的演示。在animateTransition:实现中,我尝试手动调用prefersStatusBarHidden,然后手动调用setNeedsStatusBarAppearanceUpdate以确保进行了调用,但状态栏仍然保留。

任何想法为什么会发生这种情况将不胜感激。我已经搜索过StackOverflow,但似乎没有人遇到过这个问题,所有接受的答案都涉及到调​​用setNeedsStatusBarAppearanceUpdate,这已经在做了。

编辑-现在,下面的代码似乎可以根据需要 WORK
- (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;
}

最佳答案

在iOS7中,UIViewController实际上有一个名为modalPresentationCapturesStatusBarAppearance的新属性。 Apple iOS reference.

因此,对于除普通全屏之外的任何PresentationStyle(例如:UIModalPresentationCustom),如果要捕获状态栏,则必须将此设置为。要使用它,您所要做的就是在显示的 View Controller 上将其设置为YES:

toVC.modalPresentationCapturesStatusBarAppearance = YES;

10-07 19:56
查看更多