Reputation: 715
I have implemented a slide out menu with some tinkering from this tutorial
The slide out works fine, but where I'd like my functionality to differ is when a menu option is clicked, I'd like the view on the main screen to be swapped out for another totally different view. When I swap views, I'd like to maintain the navigation bar the same because I want users to be able to continually open the slide-out menu from whatever view they are on.
What currently happens: Slide out menu works fine, but when I click on a new menu option, the view appears, but I lose the navigation bar with a left button that is supposed to allow for re-opening slide-out menu.
What currently happens. As soon as "favorites" clicked, the page displays, but left navigation button is lost (along with that functionality).
Here is how I set my code up
I have a ContainerViewController where I set up all these views:
var centerNavigationController: UINavigationController!
let homeViewController = HomeViewController()
let favoritesViewController = FavoritesViewController()
let settingsViewController = SettingsViewController()
let supportViewController = SupportViewController()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.mainWhite()
setupNavigation()
setupGestureRecognizers()
}
fileprivate func setupNavigation() {
centerNavigationController = UINavigationController(rootViewController: homeViewController)
view.addSubview(centerNavigationController.view)
addChildViewController(centerNavigationController)
centerNavigationController.didMove(toParentViewController: self)
setupLeftBarButtonItem()
centerNavigationController?.navigationBar.barTintColor = .mainGreen()
centerNavigationController?.navigationBar.tintColor = .mainGreen()
centerNavigationController?.navigationBar.isTranslucent = false
}
//Handle showing new ViewControllers...
func didSelectMenuOption(type: MenuSelection) {
switch type {
case .home:
print("home...")
case .favorites:
print("favorites...")
centerNavigationController.popViewController(animated: false)
centerNavigationController.pushViewController(favoritesViewController, animated: false)
case .settings:
print("settings...")
centerNavigationController.popViewController(animated: false)
centerNavigationController.pushViewController(settingsViewController, animated: false)
case .support:
centerNavigationController.popViewController(animated: false)
centerNavigationController.pushViewController(supportViewController, animated: false)
print("support...")
case .logout:
print("logging out...")
}
Ideally, I'd like to just "swap" out my VC's and keep that same NavBar. Is there a way to say, set the VC's in my navigation controller and then do something like this?
Upvotes: 0
Views: 198
Reputation: 759
The UINavigationController's navbar shows the buttons set by the viewController that is being displayed, so you will need to set the leftBarButtonItem when changing your viewControllers.
case .favorites:
print("favorites...")
favoritesViewController.navigationItem.leftBarButtonItem = ... // Set UIBarButtonItem
centerNavigationController.viewControllers = [favoritesViewController]
You could also implement the delegate method navigationController(_:willShow:animated:)
and set the leftBarButtonItem property on the viewController:
func navigationController(_ navigationController: UINavigationController,
willShow viewController: UIViewController,
animated: Bool) {
viewController.navigationItem.leftBarButtonItem = ... // Set UIBarButtonItem
}
Upvotes: 1