交换NavigationController的ViewController并保持相同的导航栏

时间:2018-08-17 22:38:55

标签: ios swift uiviewcontroller uinavigationcontroller

我已经实现了一个滑出菜单,并对此tutorial

进行了一些修改。

滑出效果很好,但是我希望功能有所不同的地方是单击菜单选项时,我希望将主屏幕上的视图换成另一个完全不同的视图。交换视图时,我希望导航栏保持不变,因为我希望用户能够从其打开的任何视图中连续打开滑出菜单。

当前发生的情况:滑出菜单工作正常,但是当我单击新菜单选项时,会显示视图,但是我丢失了带有应允许重新打开滑出菜单的向左按钮的导航栏

当前发生的情况。单击“收藏夹”后,即会显示页面,但左侧导航按钮会丢失(以及该功能)。

enter image description here

这是我设置代码的方式

我有一个ContainerViewController,可以在其中设置所有这些视图:

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...")


}

理想情况下,我只想“交换”我的VC,并保留相同的NavBar。有没有办法说,在导航控制器中设置VC,然后执行类似的操作?

1 个答案:

答案 0 :(得分:1)

UINavigationController的导航栏显示了由显示的viewController设置的按钮,因此在更改viewControllers时需要设置leftBarButtonItem。

case .favorites:
    print("favorites...")

    favoritesViewController.navigationItem.leftBarButtonItem = ... // Set UIBarButtonItem 

    centerNavigationController.viewControllers = [favoritesViewController] 

您还可以实现委托方法navigationController(_:willShow:animated:)并在viewController上设置leftBarButtonItem属性:

func navigationController(_ navigationController: UINavigationController, 
                         willShow viewController: UIViewController, 
                                        animated: Bool) {

     viewController.navigationItem.leftBarButtonItem = ... // Set UIBarButtonItem
}