【问题标题】:Swapping ViewControllers of NavigationController while maintaining same navbar在保持相同导航栏的同时交换 NavigationController 的 ViewController
【发布时间】:2018-08-17 22:38:54
【问题描述】:

我已经实现了一个滑出式菜单,对此tutorial 进行了一些修改

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

当前发生的情况:滑出菜单工作正常,但是当我单击新菜单选项时,会出现视图,但我会丢失导航栏,其中有一个左按钮,应该允许重新打开滑出菜单.

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

这是我设置代码的方式

我有一个 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,然后做这样的事情?

【问题讨论】:

  • 推送时需要在 viewController 上设置 left navigationItem 属性。 viewController.navigationItem.leftBarButtonItem = ...实现委托navigationController(_:willShow:animated:)并在此处设置viewController的leftBarButtonItem属性。
  • @ntsh 好的,这是有道理的.. 但就更换 VC 而言,这种方式是最好的方式吗?我应该弹出 VC 并推送而不是使用其他方式在视图控制器之间移动吗?
  • centerNavigationController.viewControllers = @[favoritesViewController] 应该可以工作。

标签: ios swift uiviewcontroller uinavigationcontroller


【解决方案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
}

【讨论】:

  • 谢谢!这行得通。很好的解决方案。只需从您的答案中删除“@”,这是一个错字。
  • @mufc 很好。我混合了objective-c语法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-29
  • 1970-01-01
  • 2019-09-17
  • 1970-01-01
  • 2016-05-22
  • 2017-09-29
相关资源
最近更新 更多