Advertisement
Don_Mag

Modify Navigation Controller stack

Jan 5th, 2023 (edited)
1,199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.85 KB | None | 0 0
  1. // embed FirstVC as the Root View Controller of a Navigation Controller
  2.  
  3. class FirstVC: UIViewController {
  4.    
  5.     override func viewDidLoad() {
  6.         super.viewDidLoad()
  7.        
  8.         view.backgroundColor = .yellow
  9.        
  10.         let button = UIButton(type: .system, primaryAction: UIAction(title: "Push to Second VC", handler: { _ in
  11.             let vc = SecondVC()
  12.             self.navigationController?.pushViewController(vc, animated: true)
  13.         }))
  14.         view.addSubview(button)
  15.         button.translatesAutoresizingMaskIntoConstraints = false
  16.         NSLayoutConstraint.activate([
  17.             button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
  18.             button.centerYAnchor.constraint(equalTo: view.centerYAnchor)
  19.         ])
  20.        
  21.     }
  22.    
  23. }
  24.  
  25. class SecondVC: UIViewController {
  26.    
  27.     override func viewDidLoad() {
  28.         super.viewDidLoad()
  29.        
  30.         view.backgroundColor = .green
  31.        
  32.         let button = UIButton(type: .system, primaryAction: UIAction(title: "Push to Third VC", handler: { _ in
  33.             let vc = ThirdVC()
  34.             self.navigationController?.pushViewController(vc, animated: true)
  35.         }))
  36.         view.addSubview(button)
  37.         button.translatesAutoresizingMaskIntoConstraints = false
  38.         NSLayoutConstraint.activate([
  39.             button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
  40.             button.centerYAnchor.constraint(equalTo: view.centerYAnchor)
  41.         ])
  42.        
  43.     }
  44.    
  45. }
  46.  
  47. class ThirdVC: UIViewController {
  48.    
  49.     override func viewDidLoad() {
  50.         super.viewDidLoad()
  51.        
  52.         view.backgroundColor = .cyan
  53.        
  54.         let v = UILabel()
  55.         v.text = "This is Third VC"
  56.         view.addSubview(v)
  57.         v.translatesAutoresizingMaskIntoConstraints = false
  58.         NSLayoutConstraint.activate([
  59.             v.centerXAnchor.constraint(equalTo: view.centerXAnchor),
  60.             v.centerYAnchor.constraint(equalTo: view.centerYAnchor)
  61.         ])
  62.        
  63.        
  64.         if let navC = self.navigationController {
  65.             var a = navC.viewControllers
  66.             a.remove(at: 1)
  67.             navC.setViewControllers(a, animated: false)
  68.         }
  69.     }
  70.    
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement