// AppDelegate.swift
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window:UIWindow?
func application(application:UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) -> Bool {
//根据一个根视图控制器创建一个导航视图控制器
let vc =ViewController()
let navc =UINavigationController(rootViewController: vc)
//将应用的根视图控制器设置为导航试图控制器
window =UIWindow(frame: UIScreen.mainScreen().bounds)
window?.backgroundColor =UIColor.whiteColor()
window?.rootViewController = navc
window?.makeKeyAndVisible()
return true
}
效果如下:
func applicationWillResignActive(application:UIApplication) {
}
func applicationDidEnterBackground(application:UIApplication) {
}
func applicationWillEnterForeground(application:UIApplication) {
}
func applicationDidBecomeActive(application:UIApplication) {
}
func applicationWillTerminate(application:UIApplication) {
}
}
// ViewController.swift
import UIKit
class ViewController: UIViewController {
overridefunc viewDidLoad() {
super.viewDidLoad()
//每一个被导航视图控制器所管理的视图控制器都有一个navigationItem(这里面包含了左按钮,右按钮,中间标题,中间视图)
//设置导航栏的标题
navigationItem.title ="Setting"
效果如下:
//设置导航栏左按钮(UIBarButtonItem)
let leftBarBtn =UIBarButtonItem(barButtonSystemItem:UIBarButtonSystemItem.Camera, target:self, action: "leftBtnAction")
navigationItem.leftBarButtonItem = leftBarBtn
//设置导航栏右按钮
let rightBarBtn =UIBarButtonItem(title: "next", style:UIBarButtonItemStyle.Plain, target:self, action: "rightBtnAction")
navigationItem.rightBarButtonItem = rightBarBtn
//设置左右Item数组
// navigationItem.leftBarButtonItems = [leftBarBtn,rightBarBtn]
// navigationItem.rightBarButtonItems = [leftBarBtn,rightBarBtn]
//设置中间视图
let segment =UISegmentedControl(items: ["已接来电","未接来电"])
segment.frame =CGRectMake(0,0, 100,30)
segment.selectedSegmentIndex =0
navigationItem.titleView = segment
效果如下:
//导航栏(UINavigationBar)
//在本类中(视图控制器)访问navigationController就是获取到本视图控制器所在的导航视图控制器
//设置导航栏是否隐藏
navigationController?.navigationBarHidden =false
//设置导航栏样式(Black,BlackTranslucent,Default)
navigationController?.navigationBar.barStyle =UIBarStyle.BlackTranslucent
效果如下:
//背景颜色
navigationController?.navigationBar.backgroundColor =UIColor.cyanColor()
效果如下:
//导航栏本身的颜色
navigationController?.navigationBar.barTintColor =UIColor.yellowColor()
//导航栏元素颜色(左按钮,右按钮。。。。)
navigationController?.navigationBar.tintColor =UIColor.redColor()
效果如下:
//导航栏半透明效果
navigationController?.navigationBar.translucent =false
let myView =UIView(frame: CGRectMake(0,0, 150,150))
myView.backgroundColor =UIColor.blueColor()
view.addSubview(myView)
navigationController?.navigationBar.translucent = false是效果如下: navigationController?.navigationBar.translucent =true是效果如下:
}
func leftBtnAction(){
println("click rightBarBtn")
}
func rightBtnAction(){
//跳转到第二个控制器页面
//1.创建第二个控制器
let secondVC =SecondViewController()
//2.使用当前控制器所在的导航视图控制器跳转到第二个控制器pushViewController(进入到下一个页面)
navigationController?.pushViewController(secondVC, animated:true)
}
overridefunc didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
// SecondViewController.swift
import UIKit
class SecondViewController:UIViewController {
overridefunc viewDidLoad() {
super.viewDidLoad()
//设置页面颜色为白色
view.backgroundColor =UIColor.whiteColor()
navigationItem.title ="SecondVC"
let leftBtn =UIBarButtonItem(title: "<返回", style:UIBarButtonItemStyle.Plain, target:self, action: "backAction")
navigationItem.leftBarButtonItem = leftBtn
let rightBarBtn =UIBarButtonItem(title: "next", style:UIBarButtonItemStyle.Plain, target:self, action: "nextAction")
navigationItem.rightBarButtonItem = rightBarBtn
//模态测试按钮
let motaiBtn =UIButton(frame: CGRectMake(130,150, 100,40))
motaiBtn.setTitle("模态测试", forState:UIControlState.Normal)
motaiBtn.setTitleColor(UIColor.blueColor(), forState:UIControlState.Normal)
motaiBtn.setTitleColor(UIColor.redColor(), forState:UIControlState.Highlighted)
motaiBtn.layer.borderColor =UIColor.blueColor().CGColor
motaiBtn.layer.borderWidth =1
motaiBtn.layer.cornerRadius =10
motaiBtn.addTarget(self, action:"presentToSix", forControlEvents:UIControlEvents.TouchUpInside)
view.addSubview(motaiBtn)
}
func backAction(){
//将secondVC出栈
//popViewControllerAnimated:将当前显示在栈顶的控制器出栈(返回上一个页面)
navigationController?.popViewControllerAnimated(true)
//返回到根视图控制器
//(1).popToRootViewControllerAnimated(true)直接返回根视图控制器
//navigationController?.popToRootViewControllerAnimated(true)
//(2).先获取到栈里所有的视图控制器
let viewControllers =navigationController?.viewControllers
//获取根视图控制器(因为根视图控制器是最先入栈,所以在第0个下标)
let rootVC:AnyObject = viewControllers![0]
//导航视图控制器返回到指定的视图控制器
navigationController?.popToViewController(rootVCas! UIViewController, animated:true)
}
func nextAction(){
let thirdVC =ThirdViewController()
navigationController?.pushViewController(thirdVC, animated:true)
}
//点击按钮模态显示第六个视图控制器
func presentToSix(){
let sixthVC =SixthViewController()
//模态显示,跟导航视图控制器没关系
//参数completion:模态显示完成后要执行的闭包
presentViewController(sixthVC, animated:true) { () -> Voidin
//模态显示动作完成要执行的代码
//println("模态动作已完成")
}
}
overridefunc didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
// SixthViewController.swift
import UIKit
class SixthViewController:UIViewController {
overridefunc viewDidLoad() {
super.viewDidLoad()
view.backgroundColor =UIColor.cyanColor()
//模态消失测试按钮
let backBtn =UIButton(frame: CGRectMake(130,200, 100,40))
backBtn.setTitle("完成", forState:UIControlState.Normal)
backBtn.setTitleColor(UIColor.blackColor(), forState:UIControlState.Normal)
backBtn.setTitleColor(UIColor.redColor(), forState:UIControlState.Highlighted)
backBtn.layer.cornerRadius =10
backBtn.layer.borderWidth =1
backBtn.layer.borderColor =UIColor.blackColor().CGColor
backBtn.addTarget(self, action:"dismissViewController", forControlEvents:UIControlEvents.TouchUpInside)
view.addSubview(backBtn)
}
func dismissViewController(){
//1.第一种方式:dismissViewController()模态消失过程不可定制化
//2.第二种方式:模态小时过程可定制化(需不需要动画,模态结束后执行代码段)
dismissViewControllerAnimated(true, completion: { () ->Void in
//println("模态消失动作已结束")
})
}
overridefunc didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}