0

I have several views in my app, and I only want a navigationbar on one of them.... I used a navigationcontroller and at first I was using this code (while my app was in its infancy and only had 2 views)

override func viewWillAppear(_ animated: Bool) {
    self.navigationController?.setNavigationBarHidden(true, animated: animated)
    super.viewWillAppear(animated)
}

override func viewWillDisappear(_ animated: Bool) {
    self.navigationController?.setNavigationBarHidden(false, animated: animated)
    super.viewWillDisappear(animated)
}

It worked fine - however, the app has become more complex - I have these views

lazy var orderedViewControllers: [UIViewController] = {
    return [self.newVc(viewController: "pageOne"),
            self.newVc(viewController: "pageTwo"),
            self.newVc(viewController: "pageThree"),
            self.newVc(viewController: "pageFour"),
            self.newVc(viewController: "activate")
    ]
}()

Where this code isn't applied to, even if I create a custom view controller for each view.

I thought the way to do this would be to put the top chunk of code in every view, but it's not working for the bottom chunk. In essence my question is how do I use NavigationController to create a bar ONLY on one view.

Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
David
  • 31
  • 7
  • Hi David, Can you explain bit more, Which functionality do you need? – Rishil Patel Dec 05 '18 at 18:43
  • Yes. I have several views. My main view, has a button to go to a Settings page. This settings page is to have a top navigation bar with a back button to go back to the main page. I achieved this functionality with a UINavigationView - However, any page I navigate to from the main controller now has this top navigation bar. I *only* want the settings page to have the navigation bar. – David Dec 05 '18 at 18:45
  • Did you check this link : https://stackoverflow.com/questions/845583/iphone-hide-navigation-bar-only-on-first-page – Rishil Patel Dec 05 '18 at 18:53
  • Yes like I said, I managed to get it working on one page but not those when the carousel view is used. – David Dec 05 '18 at 18:54
  • Can you add image of that view which you have an issue? – Rishil Patel Dec 05 '18 at 19:01

3 Answers3

1

One option: use a "base view controller" class which handles hiding / showing the Navigation Bar, and make your "pages" sub-classes of the "base" class.

import UIKit

class BaseViewController: UIViewController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: animated)
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: animated)
    }

}

class ViewController: UIViewController {

    // has buttons with
    //    Show (e.g. push)
    // segues to Settings, First, Second, Third view controllers

}

class SettingsViewController: UIViewController {

    // Settings VC is a normal UIViewController, because
    // we *want* the NavBar to remain visible

}

class FirstViewController: BaseViewController {
    @IBAction func backTapped(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
}

class SecondViewController: BaseViewController {
    @IBAction func backTapped(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
}

class ThirdViewController: BaseViewController {
    @IBAction func backTapped(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
}
DonMag
  • 69,424
  • 5
  • 50
  • 86
0

You can use this method of UINavigationControllerDelegate

optional func navigationController(_ navigationController: UINavigationController, 
                          willShow viewController: UIViewController, 
                          animated: Bool){
     if viewController == self."desired view controller" {
        self.isNavigationBarHidden = true
    }else{
        self.isNavigationBarHidden = false 
    }
}
Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
-1

Thank you all for the support. I have resolved my issue by doing the following:

  1. I put the only view controller I wanted to have a navigation bar in a navigation controller view the Embed menu.
  2. I added a custom back button.
David
  • 31
  • 7