1.UINavigationController(导航控制器)的介绍
1)viewController属性
该属性保存着UINavigationController栈中视图控制器的信息。
2)UINavigationController的两个视图
A:UINavigationBar
当某个UIViewController对象成为UINavigationController的栈顶对象时,UINavigationBar对象会访问该UIViewController对象的navigationItem属性,获取界面显示的相关内容
B:topViewController
该属性是UINavigationController栈顶上的视图控制器,当前所处的视图控制器。
3)navigationItem:UINavigationItem
UINavigationItem对象的作用是为UINavigationBar提供绘图需要的内容。
UINavigationController扩展UIViewController了,增加了navigationItem的属性。该属性包括leftBarButtonItem,titleView,rightBarButtonItem,而它们又是UIBarButtonItem对象。
2.UINavigationController的使用(demo如下)
Demo中包括了首页,第二页,第三页之间的跳转以及从首页navigationBar的设置。
// // AppDelegate.swift import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? //在显示App给用户之前执行的最后的初始化操作 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { window = UIWindow(frame: UIScreen.mainScreen().bounds) window?.backgroundColor = UIColor.whiteColor() let nav = UINavigationController(rootViewController: ViewController()) window?.rootViewController = nav window?.makeKeyAndVisible() //导航栏上的背景色 UINavigationBar.appearance().barTintColor = UIColor(red: 33/255, green: 150/255, blue: 243/255, alpha: 1) //textTitle的颜色 UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()] //导航栏上的图标的颜色 UINavigationBar.appearance().tintColor = UIColor.whiteColor() return true } //App将要从foreground切换到background需要执行的操作 func applicationWillResignActive(application: UIApplication) { } //App已经进行background后需要执行的操作 func applicationDidEnterBackground(application: UIApplication) { } //App将要从background进行foreground需要执行的操作,此时应用程序的状态不是Active func applicationWillEnterForeground(application: UIApplication) { } //App切换到Active需要执行的操作 func applicationDidBecomeActive(application: UIApplication) { } //App将要终止执行的操作 func applicationWillTerminate(application: UIApplication) { } }
// ViewController.swift import UIKit class ViewController: UIViewController { var button = UIButton() let width = UIScreen.mainScreen().bounds.width let height = UIScreen.mainScreen().bounds.height override func viewDidLoad() { super.viewDidLoad() //设置title self.navigationItem.title = "首页" //设置跳转的按钮 button = UIButton(frame: CGRectMake(width/2,height/2,100,50)) button.setTitle("下一页", forState: UIControlState.Normal) button.backgroundColor = UIColor.redColor() button.addTarget(self, action: "jump", forControlEvents: UIControlEvents.TouchUpInside) //添加leftBarButton let leftButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "Add") self.navigationItem.leftBarButtonItem = leftButton let rightButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Edit, target: self , action: "Edit") self.navigationItem.rightBarButtonItem = rightButton self.view.addSubview(button) } func jump(){ let secVC = SecondViewController() self.navigationController?.pushViewController(secVC, animated: true) } func Add(){ print("Add") } func Edit(){ print("Edit") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
// // SecondViewController.swift import UIKit class SecondViewController: UIViewController { var button = UIButton() let width = UIScreen.mainScreen().bounds.width let height = UIScreen.mainScreen().bounds.height override func viewDidLoad() { super.viewDidLoad() self.navigationItem.title = "第二页" button = UIButton(frame: CGRectMake(width/2,height/2,100,50)) button.setTitle("下一页", forState: UIControlState.Normal) button.backgroundColor = UIColor.grayColor() button.addTarget(self, action: "jump", forControlEvents: UIControlEvents.TouchUpInside) self.view.addSubview(button) } func jump(){ let thiVC = ThirdViewController() self.navigationController?.pushViewController(thiVC, animated: true) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
// // ThirdViewController.swift import UIKit class ThirdViewController: UIViewController { var button = UIButton() let width = UIScreen.mainScreen().bounds.width let height = UIScreen.mainScreen().bounds.height override func viewDidLoad() { super.viewDidLoad() self.navigationItem.title = "最后一页" button = UIButton(frame: CGRectMake(width/2,height/2,100,50)) button.setTitle("回到首页", forState: UIControlState.Normal) button.backgroundColor = UIColor.blueColor() button.addTarget(self, action: "jump", forControlEvents: UIControlEvents.TouchUpInside) self.view.addSubview(button) } func jump(){ self.navigationController?.popToRootViewControllerAnimated(true) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }