user7097242
user7097242

Reputation: 1074

How to hide status bar in specific view controller?

I have 3 view controllers which are added to a pageViewController so I can scroll between the 3. The issue is I want to display the status bar in only 1 of the viewControllers. So far I can hide from them all or show in them all.

I tried the following:

    private var isStatusBarHidden = false {
    didSet {
        setNeedsStatusBarAppearanceUpdate()
    }
}

    override var prefersStatusBarHidden: Bool {
        return isStatusBarHidden
}

How I added the VC's as child view controllers to my scroll view:

 let storyboard = UIStoryboard(name: "Main", bundle: nil)

    page1 = storyboard.instantiateViewController(withIdentifier: StoryboardIdentifiers.feedViewController.rawValue) as! FeedViewController
    page1.view.translatesAutoresizingMaskIntoConstraints = false
    page1.delegate = self
    scrollView.addSubview(page1.view)
    addChildViewController(page1)
    page1.didMove(toParentViewController: self)

Upvotes: 1

Views: 1157

Answers (1)

Abhishek Sharma
Abhishek Sharma

Reputation: 475

You have 3 VC means all 3 ViewController is going to have viewDidAppear and viewWillDisappear code

 override open func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(true)
        //It will show the status bar again after dismiss
        UIApplication.shared.isStatusBarHidden = true
    }


override open func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    //It will hide the status bar again after dismiss
    UIApplication.shared.isStatusBarHidden = false
}
override open var prefersStatusBarHidden: Bool {
    return true
}

Copy and paste code into those ViewController in which you want to hide your status bar. So what it will do is inside your viewDidAppear it will hide your status bar and as soon as we will leave the class it will set status bar visible.

In case your pageViewController is parent view then We can do it via page index Let suppose you want to show status bar on page 2 and hide on page 1 and 3. So we can do it, in this page

PageDataSource Function {
   if(index == 1 || index == 3){
     UIApplication.shared.isStatusBarHidden = true
   }
   else{
     UIApplication.shared.isStatusBarHidden = false
  }
}

override open var prefersStatusBarHidden: Bool {
    return true
}

  override open func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    //It will hide the status bar again after dismiss
    UIApplication.shared.isStatusBarHidden = false
}

Please try this and let me know if its working or not

Thank you

Upvotes: 2

Related Questions