1

I would like to hide the status bar text without removing the status bar itself.

I am presenting a new view controller over full screen and I don't want the status bar text to be visible when that view controller has taken over the full screen. Note that the presenting view controller has a UINavigationBar while the presented view controller does not.

I have tried simply returning true in prefersStatusBarHidden but that causes the status bar frame to be removed, causing the navigation controller on the presenting view controller to slide up, and this is visible while the new view controller is still animating up from the bottom.

I then tried setting modalPresentationCapturesStatusBarAppearance to true for the presented view controller, then return false in prefersStatusBarHidden and .Fade in preferredStatusBarUpdateAnimation, calling setNeedsStatusBarAppearanceUpdate() in viewDidAppear but this results in the status bar text changing from white to black and remaining visible. Setting prefersStatusBarHidden to true removes the status bar like I mentioned before.

Ideally, I would start fading the text on the status bar when I initiate the segue, and the alpha would reach 0 when the animation completes. Then upon dismissal fade back to alpha 1. Is this or a similar solution possible? I only have to support iOS 8+.

4
  • "Note that the presenting view controller has a UINavigationBar while the presenting view controller does not." Do you realize that that is a contradiction? From the title, this sound like a really simple question, but your wording has made this really difficult. Please review what you've written. Sep 8, 2014 at 0:09
  • @doctordoder the second one should have been "presented" - fixed
    – Jordan H
    Sep 8, 2014 at 2:12
  • Is what you are trying to accomplish just to not have the navigation bar of the presenting view controller slide up due to the status bar hiding when you present the view controller? (It sounds like the presented view controller should not have a status bar, but you are trying to keep the status bar while making it invisible just for the sake of the present/dismiss animation.)
    – ememem
    Oct 4, 2014 at 5:46
  • @user2135004 Yes exactly.
    – Jordan H
    Oct 4, 2014 at 5:49

2 Answers 2

1

Actually, you can access the window with status bar directly and do with it whatever you want.

- (UIWindow *)statusBarWindow
{
    return (UIWindow *)[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"];
}

Of course, it is undocumented feature and it can be dangerous in terms of App Review Process, but I have only positive experience with it.

1

Solution 1: The solution I have is very hacky, but it works. First, the presented view controller should have modalPresentationCapturesStatusBarAppearance set to YES and should return YES for prefersStatusBarHidden.

Now, the problem is just not to have the navigation bar slide up. To do this, first subclass UINavigationBar and override the setFrame:, setBounds:, and setCenter: to keep the shape and position of the navigation bar fixed even when it wants to slide up when the status bar hides. Something like this:

const CGFloat kStatusBarHeight = 20;
const CGFloat kDefaultNavigationBarHeight = 44;
const CGFloat kLandscapeNavigationBarHeight = 32;

...

- (void)setCenter:(CGPoint)center {
    center = [self forcedCenterForCenter:center];
    [super setCenter:center];
}

- (void)setBounds:(CGRect)bounds {
    bounds = [self forcedBoundsForBounds:bounds];
    [super setBounds:bounds];
}

- (void)setFrame:(CGRect)frame {
    frame = [self forcedFrameForFrame:frame];
    [super setFrame:frame];
}

- (CGPoint)forcedCenterForCenter:(CGPoint)center {
    if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) {
        center.y = kStatusBarHeight + kDefaultNavigationBarHeight * 0.5;
    }
    else {
        // No status bar in landscape orientation in iOS 8.
        center.y = kLandscapeNavigationBarHeight * 0.5;
    }

    return center;
}

- (CGRect)forcedBoundsForBounds:(CGRect)bounds {
    if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) {
        bounds.size.height = kDefaultNavigationBarHeight;
    }
    else {
        bounds.size.height = kLandscapeNavigationBarHeight;
    }

    return bounds;
}

- (CGRect)forcedFrameForFrame:(CGRect)frame {
    if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) {
        frame.origin.y = kStatusBarHeight;
        frame.size.height = kDefaultNavigationBarHeight;
    }
    else {
        // No status bar in landscape orientation in iOS 8.
        frame.origin.y = 0.0;
        frame.size.height = kLandscapeNavigationBarHeight;
    }

    return frame;
}

Let's call this class ForcedNavigationBar. If you just pass this class into UINavigationController's initWithNavigationBarClass:toolbarClass:, the navigation bar will stay in place, but you run into other problems. First, sometimes, the navigation bar will not extend upward to underlap the status bar. Second, a view controller pushed onto the navigation controller shouldn't use its topLayoutGuide property anymore to do layout because topLayoutGuide may take into account the status bar having actually been hidden even though we are always forcing a space for an imaginary status bar.

To solve the first problem, first make ForcedNavigationBar's background invisible by setting its backgroundColor property to [UIColor clearColor] and overriding drawRect: to do absolutely nothing (no calls to super). (The buttons of the navigation bar will still be visible.) Now, we place a dummy navigation bar under the real navigation bar to provide a background that will always underlap the space for an imaginary status bar. We can do this by subclassing UINavigationController (call it ForcedNavigationBar) and adding constraints to the dummy navigation bar:

- (void)viewDidLoad {
    [super viewDidLoad];

    UINavigationBar * backgroundNavigationBar = [[UINavigationBar alloc] init];
    [self.navigationBar.superview insertSubview:backgroundNavigationBar belowSubview:self.navigationBar];
    {
        backgroundNavigationBar.translatesAutoresizingMaskIntoConstraints = NO;

        // The background bar always hugs the top of the screen.
        NSLayoutConstraint * topConstraint = [NSLayoutConstraint constraintWithItem:backgroundNavigationBar
                                                                          attribute:NSLayoutAttributeTop
                                                                          relatedBy:NSLayoutRelationEqual
                                                                             toItem:self.view
                                                                          attribute:NSLayoutAttributeTop
                                                                         multiplier:1
                                                                           constant:0];
        [self.view addConstraint:topConstraint];

        NSLayoutConstraint * bottomConstraint = [NSLayoutConstraint constraintWithItem:backgroundNavigationBar
                                                                             attribute:NSLayoutAttributeBottom
                                                                             relatedBy:NSLayoutRelationEqual
                                                                                toItem:self.navigationBar
                                                                             attribute:NSLayoutAttributeBottom
                                                                            multiplier:1
                                                                              constant:0];
        [self.navigationBar.superview addConstraint:bottomConstraint];

        NSLayoutConstraint * leftConstraint = [NSLayoutConstraint constraintWithItem:backgroundNavigationBar
                                                                           attribute:NSLayoutAttributeLeft
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:self.navigationBar
                                                                           attribute:NSLayoutAttributeLeft
                                                                          multiplier:1
                                                                            constant:0];
        [self.navigationBar.superview addConstraint:leftConstraint];

        NSLayoutConstraint * rightConstraint = [NSLayoutConstraint constraintWithItem:backgroundNavigationBar
                                                                            attribute:NSLayoutAttributeRight
                                                                            relatedBy:NSLayoutRelationEqual
                                                                               toItem:self.navigationBar
                                                                            attribute:NSLayoutAttributeRight
                                                                           multiplier:1
                                                                             constant:0];
        [self.navigationBar.superview addConstraint:rightConstraint];
    }
}

To solve the second problem, you just have to not use topLayoutGuide and set the space at the top based on where you intend the navigation bar to be positioned. For views that are not scroll views, you can update the constraints that position them relative to the navigation bar in viewWillLayoutSubviews. If you have a scroll view (like a table view), you can adjust the top part of its contentInset in viewDidLayoutSubviews. (Previously, you would have set the top part of the scroll view's contentInset to topLayoutGuide.length.)

The code I've provided would make this work on an iPhone 5 screen. In iOS 8, you'll have to make this code "adaptive," so it will still work on an iPhone 6 screen. For example, it's probably the case that, in landscape orientation, the navigation bar is not squashed on the iPhone 6.

EDIT: Solution 2: Use snapshotViewAfterScreenUpdates to take a snapshot view of the presenting view controller and insert it in the view hierarchy before presentation or dismissal. That way, you don't have to worry about the navigation bar sliding up or down. (I have not personally tried this, but it seems promising.)

EDIT 2: One little wrinkle to this solution is that the navigation bar will not have a space for the status bar right before dismissal. If you are using a UIViewControllerAnimatedTransitioning for dismissal, calling [UIView animationWithDuration:...] with an empty animation block seems to (somehow) force the system to have the "to" view controller (the navigation controller) dictate the status bar appearance. Then to get the "to" view controller to layout with a space for the status bar, call layoutIfNeeded on maybe the container view or some view with the view of "to" view controller in it. Do all this in animateTransition: but before you actually do the animations in this method.

2
  • Indeed that is quite hackish. Nice work, although personally I'd rather deal with a sliding status bar than implement magic code. :) I wonder if there's other solutions, any way to enforce a view controller appear overtop the status bar and therefore I could just leave it always visible? Any way to change the alpha without removing the status bar? etc
    – Jordan H
    Oct 4, 2014 at 17:04
  • @Joey Another idea to try is to take a snapshot of the presenting view controller to be a stand-in for it during the presentation/dismissal animations. That way, you don't have to worry about the bar sliding up or down.
    – ememem
    Oct 4, 2014 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.