Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show/Hide status bar in only one ViewController, objective C, iOS

I want status bar to show up in viewWillAppear() and disappear in viewWillDisappear() of my ViewController

I was using

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];

but it is deprecated in iOS 9.0

I am using

UIApplication.shared.isStatusBarHidden = false

in swift, but in objective C this is readonly value...

prefersStatusBarHidden also does not work for me, because I need to hide status bar in viewWillDisappear() function

-(BOOL)prefersStatusBarHidden{
    return YES;
}

Can anybody help me?

like image 285
Salome Tsiramua Avatar asked Oct 24 '16 16:10

Salome Tsiramua


2 Answers

For each view controller you want to change the visibility of the status bar you need to override prefersStatusBarHidden. For this to actually work though, you must add the following key/value in your project's Info.plist:

Key: View controller-based status bar appearance

Value: YES


To control the visibility of the status bar in viewWillAppear and viewWillDisappear you can do:

var statusBarHidden = false

override func viewWillAppear() {
    super.viewWillAppear()
    statusBarHidden = false
    self.setNeedsStatusBarAppearanceUpdate()
}

override func viewWillDisappear() {
    super.viewWillDisappear()
    statusBarHidden = true
    self.setNeedsStatusBarAppearanceUpdate()
}

override var prefersStatusBarHidden: Bool {
    return statusBarHidden
}
like image 74
dennykim Avatar answered Nov 16 '22 06:11

dennykim


For Swift 3,

override var prefersStatusBarHidden: Bool{
        return true
    }

and add viewDidLoad()

self.modalPresentationCapturesStatusBarAppearance = true
like image 28
Ali BOZOĞLU Avatar answered Nov 16 '22 06:11

Ali BOZOĞLU