1

I am using a UINavigationController.

Lets say I have these stacks

VC1 -> VC2 -> VC3 
VC1 -> VC3
VC1 -> VC2 -> VC4 -> VC3

In all cases, how can I dismiss all VC's below VC3 when it is the current view controller.

VC3 is connected to a TabBarController if this make a difference.

Luca Sarif
  • 142
  • 2
  • 13
  • you want VC3 `TabBarController` as root View and remove the whole `UINavigationController` with VC1 and VC2 ? – Amit Aug 01 '18 at 12:48
  • pretty much yh @Amit – Luca Sarif Aug 01 '18 at 12:49
  • you can directly change the root View controller by adding `self.view.window?.rootViewController = TabBarController` in VC2. – Amit Aug 01 '18 at 12:51
  • Since you have navigation controller, what do you mean by "dismiss" the view controllers? Why do you need to do this? – Ahmad F Aug 01 '18 at 12:52
  • I dont want the user to swipe back from VC3, but I can't disable the gesture recognizer on individual VC's. I will try your suggestion thank you. – Luca Sarif Aug 01 '18 at 12:53

3 Answers3

1

From VC2, use setViewControllers method to push VC3 and to remove the rest,

self.navigationController?.setViewControllers([VC3], animated: true)
Kamran
  • 14,987
  • 4
  • 33
  • 51
0

You can remove a view controller from the navigation controller stack like that: self.navigationController?.viewControllers.remove(at: index)

Todor Brachkov
  • 219
  • 1
  • 9
  • sorry i didnt specify in question but the number of VC's below VC3 is ambiguos. There can be 1 view controller or 3 view controllers – Luca Sarif Aug 01 '18 at 12:50
  • In that case all you need to do is just set the VC3 as the only VC, like the answer below. Basically you should set the array by setting `setViewControllers` with the top most VC in your case the VC3. – Todor Brachkov Aug 01 '18 at 13:00
0

In your scenario it's better to change the application's root view from UINavigationController to UITabBarcontroller.

You can achieve that by doing in any VC:

self.view.window?.rootViewController = TabBarController

To know more about it you can check this link:

Amit
  • 4,837
  • 5
  • 31
  • 46