Search code examples
iosswiftstatusbar

Hiding a statusbar in specifc viewcontroller


I'm trying to achieve the same thing as when you show a picture in facebook where the statusBar fade. i've tried below but nothing seem to happen, what can i do in order to fade the statusBar out?

override func viewDidLoad() {
    self.view.backgroundColor = UIColor.blackColor()

    //Set ImageView
    imageView.image = galleryImage

    self.setNeedsStatusBarAppearanceUpdate()

}

override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
    return UIStatusBarAnimation.Fade
}
override func prefersStatusBarHidden() -> Bool {
    return true
}

Solution

  • In your info.plist add row called View controller-based status bar appearance and set it to NO .

    Then call the following code in viewDidload and it should hide with animation:

    UIApplication.sharedApplication().statusBarHidden = true
    

    After that you will be getting a warning printed in the console like :

    CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
    

    Just ignore it, as they said its a bug from apple and for reference see here: How can I set CG_CONTEXT_SHOW_BACKTRACE environmental variable?