Override back button in navigation stack while keeping appearance of default back button?

后端 未结 8 2077
野的像风
野的像风 2020-12-08 22:38

How do I override the back button for just one view (not for all the back buttons present in different views) such that on click of the back button, root view controller is

8条回答
  •  春和景丽
    2020-12-08 23:04

    It's old, but the correct answer is that:

    Instead of pushing your ViewController on top of all the others, you'd better replace the full stack with the rootVC and the new VC only.

    Not:

    self.navigationController?.pushViewController(myVc, animated: true)
    

    But:

    let vcStack = self.navigationController?.viewControllers
    self.navigationController?.setViewControllers([vcStack![0],myVc], animated: true)
    

    Like that, on back it will just popToRoot because it's the previous viewController in stack

提交回复
热议问题