6

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?

3
  • Great question! I've been struggling with these too recently! Commented Oct 24, 2016 at 16:38
  • You can try to add the key "UIViewControllerBasedStatusBarAppearance" with value "NO" in the info.plist, also call the func setNeedsStatusBarAppearanceUpdate() if need. Commented Oct 24, 2016 at 17:35
  • I have "UIViewControllerBasedStatusBarAppearance - NO " in the info.plist, I need the translate "UIApplication.shared.isStatusBarHidden = false" swift code to objective C Commented Oct 25, 2016 at 8:44

3 Answers 3

5

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes it will work definitely, but I want to avoid overriding this method in each view controller and I know it is possible in swift and suppose that would be possible in objective C too, but cannot find the correct function
To avoid overriding this method in each view controller, you can subclass UIViewController with these methods and use the subclass where appropriate.
1

For Swift 3,

override var prefersStatusBarHidden: Bool{
        return true
    }

and add viewDidLoad()

self.modalPresentationCapturesStatusBarAppearance = true

2 Comments

This is the right answer! But I did not need the statement in viewDidLoad(). It works without it.
self.modalPresentationCapturesStatusBarAppearance = true is the right for answer for modal view controller.
1

Write example for Objective-C (The same code for SWIFT already wrote by @dennykim)

  1. Create property for BOOL

    @property (nonatomic,assign) BOOL statusBarHidden;
    
  2. Set in info.plist View controller-based status bar appearance == YES

  3. Go to ViewController and write the next code:

    -(void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
    
        self.statusBarHidden = TRUE;
        [self setNeedsStatusBarAppearanceUpdate];
    }
    
    -(void)viewWillDisappear:(BOOL)animated{
        [super viewWillDisappear:animated];
    
        self.statusBarHidden = FALSE;
        [self setNeedsStatusBarAppearanceUpdate];
    }
    
    - (BOOL)prefersStatusBarHidden {
        return self.statusBarHidden;
    }
    

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.