1

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)
user7097242
  • 1,034
  • 3
  • 16
  • 31

1 Answers1

2

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

Abhishek Sharma
  • 475
  • 3
  • 13
  • Before I try I should mention in my plist i have this: View controller-based status bar appearance = NO Status bar is initially hidden = NO Do I need to change this at all? – user7097242 Jan 26 '17 at 20:16
  • I don't think it's required to mention in plist but you can. Because In my application what I did is, set View controller-based status bar appearance = YES because I have to show the status bar in the whole app. But when your opens photo I need to hide you I just wrote the code written above And It's working fine. – Abhishek Sharma Jan 26 '17 at 20:19
  • I tried the first part and it didn't work. It seems like it keeps adding the status bar to the superview. The second part I don't understand that function you created ( really new to this!) =] – user7097242 Jan 26 '17 at 20:29
  • Wait I am trying to implement it by making a demo project. I'll get back to you soon – Abhishek Sharma Jan 26 '17 at 20:33
  • Please check the project https://www.dropbox.com/s/hrlf5dp8hks8cu4/UIPageViewController.zip?dl=0 – Abhishek Sharma Jan 26 '17 at 20:41
  • Class Name TutorialViewController Check extension code in this class I have written same logic – Abhishek Sharma Jan 26 '17 at 20:42
  • Thanks for sending that! Since I am not using a UIPageVIewControl but instead have a container view which holds the 3 VCs I am switching from, how would I go about it now? – user7097242 Jan 26 '17 at 20:47
  • Okay How are you loading those VC. Please explain that function. Based on index or some logic to load those 3 VC – Abhishek Sharma Jan 26 '17 at 20:48
  • I have the code I added above but for 3 of them inside viewDidLoad – user7097242 Jan 26 '17 at 20:54
  • If possible please provide a demo project on git or dropbox. I'll try my best to implement status bar code. – Abhishek Sharma Jan 26 '17 at 20:56
  • To anyone looking at this post, once you have implemented the solution provided by Abhishek, you should also set "View controller-based status bar appearance = NO" flag in the Info.plist file for the settings to take effect. Because it worked in my case only after I did that. Thanks! – programmer Dec 22 '17 at 09:00