This is the sample code for the NavigationContoroller.
The NavigationContoroller moves sideways to make screen transitions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var navigationController: UINavigationController?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window!.makeKeyAndVisible()
let firstViewController: FirstSwitchNavigationViewController? = FirstSwitchNavigationViewController()
navigationController = UINavigationController(rootViewController: firstViewController!)
window!.rootViewController = navigationController
return true
}
func applicationWillResignActive(_ application: UIApplication) {
}
func applicationDidEnterBackground(_ application: UIApplication) {
}
func applicationWillEnterForeground(_ application: UIApplication) {
}
func applicationDidBecomeActive(_ application: UIApplication) {
}
func applicationWillTerminate(_ application: UIApplication) {
}
}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit
class FirstSwitchNavigationViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
title = "FirstView"
view.backgroundColor = UIColor.red
let width = view.frame.width
let height = view.frame.height
let transitionBtn = UIButton()
transitionBtn.frame.size = CGSize(width: width * 0.6, height: height * 0.2)
transitionBtn.center = view.center
transitionBtn.backgroundColor = UIColor.blue
transitionBtn.setTitle("Switch to SecondVC", for: UIControl.State.normal)
transitionBtn.addTarget(self, action: #selector(goSevondVC(sender:)), for:.touchUpInside)
view.addSubview(transitionBtn)
}
@objc internal func goSevondVC(sender: UIButton){
let secondVC = SecondSwitchNavigationViewController()
navigationController?.setViewControllers([secondVC], animated: true)
}
}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit
class SecondSwitchNavigationViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
title = "SecondView"
view.backgroundColor = UIColor.blue
let width = view.frame.width
let height = view.frame.height
let transitionBtn = UIButton()
transitionBtn.frame.size = CGSize(width: width * 0.6, height: height * 0.2)
transitionBtn.center = view.center
transitionBtn.backgroundColor = UIColor.green
transitionBtn.setTitle("Switch to FirstVC", for: UIControl.State.normal)
transitionBtn.setTitleColor(UIColor.black, for: UIControl.State.normal)
transitionBtn.addTarget(self, action: #selector(backToFirstVC(sender:)), for:.touchUpInside)
view.addSubview(transitionBtn)
}
@objc internal func backToFirstVC(sender: UIButton){
let firstVC = FirstSwitchNavigationViewController()
navigationController?.setViewControllers([firstVC], animated: true)
}
}