import Foundation import UIKit extension UINavigationController { func replaceTopViewController(with viewController: UIViewController, animated: Bool) { var vcs = viewControllers vcs[vcs.count - 1] = viewController UIView.transition(with: self.view, duration: 0.75, options: .transitionFlipFromTop, animations: { [weak self] in self?.setViewControllers(vcs, animated: false) }, completion: nil) } func popToViewController(ofClass: AnyClass, animated: Bool = true) { if let vc = viewControllers.filter({$0.isKind(of: ofClass)}).last { popToViewController(vc, animated: animated) } } func replaceViewController(with viewController: UIViewController, ofClass: AnyClass){ var vcs = viewControllers if let vc = viewControllers.filter({$0.isKind(of: ofClass)}).last { if let index = vcs.index(of: vc){ vcs[index] = viewController UIView.transition(with: self.view, duration: 0.75, options: .transitionFlipFromTop, animations: { [weak self] in self?.setViewControllers(vcs, animated: false) }, completion: nil) } } } }