UIViewController's prefersStatusBarHidden not working

Taz picture Taz · May 12, 2014 · Viewed 37.1k times · Source

I am trying to make the status bar for one of my view controllers to be hidden (when displayed modally). When I'm presenting the view controller, the status bar is is to be hidden and then returned when dismissed.

I have added the following code to the presented view controller

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

I have also set the keys in the Info.plist file to the following:

<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>

From my understanding this should be all that is required to make this work.

I am also using a custom Animation Controller to do the presenting which conforms to the UIViewControllerAnimatedTransitioning protocol. In the animateTransition: implementation I have tried to manually call prefersStatusBarHidden, followed by setNeedsStatusBarAppearanceUpdate to ensure the call is being made, but the status bar remains.

Any ideas why this is happening would be appreciated. I have searched StackOverflow, but it appears no one has had this issue, all accepted answers refer to calling setNeedsStatusBarAppearanceUpdate, which I am already doing.

EDIT - The code below now seems to WORK as desired

- (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;
}

Answer

David Liu picture David Liu · Aug 15, 2014

In iOS7, there's actually a new property for UIViewController called modalPresentationCapturesStatusBarAppearance. Apple iOS reference.

Default value is NO.

When you present a view controller by calling the presentViewController:animated:completion: method, status bar appearance control is transferred from the presenting to the presented view controller only if the presented controller’s modalPresentationStyle value is UIModalPresentationFullScreen. By setting this property to YES, you specify the presented view controller controls status bar appearance, even though presented non–fullscreen.

The system ignores this property’s value for a view controller presented fullscreen.

Therefore, for any presentationStyle other than the normal fullscreen (for example: UIModalPresentationCustom), this must be set if you want to capture the status bar. To use, all you have to do is set it to YES on the view controller that's being presented:

toVC.modalPresentationCapturesStatusBarAppearance = YES;