Search code examples
iosswiftstatusbaralpha

Fade status bar text without removing status bar itself


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+.


Solution

  • 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.