ExtensionUINavigationController.swift 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import Foundation
  2. import UIKit
  3. extension UINavigationController {
  4. func replaceTopViewController(with viewController: UIViewController, animated: Bool) {
  5. var vcs = viewControllers
  6. vcs[vcs.count - 1] = viewController
  7. UIView.transition(with: self.view, duration: 0.75, options: .transitionFlipFromTop, animations: { [weak self] in
  8. self?.setViewControllers(vcs, animated: false)
  9. }, completion: nil)
  10. }
  11. func popToViewController(ofClass: AnyClass, animated: Bool = true) {
  12. if let vc = viewControllers.filter({$0.isKind(of: ofClass)}).last {
  13. popToViewController(vc, animated: animated)
  14. }
  15. }
  16. func replaceViewController(with viewController: UIViewController, ofClass: AnyClass){
  17. var vcs = viewControllers
  18. if let vc = viewControllers.filter({$0.isKind(of: ofClass)}).last {
  19. if let index = vcs.index(of: vc){
  20. vcs[index] = viewController
  21. UIView.transition(with: self.view, duration: 0.75, options: .transitionFlipFromTop, animations: { [weak self] in
  22. self?.setViewControllers(vcs, animated: false)
  23. }, completion: nil)
  24. }
  25. }
  26. }
  27. }