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?.rootViewController = navc
    window?.backgroundColor = UIColor.whiteColor()
    window?.makeKeyAndVisible()
    return true
}

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    
    //每一个被导航视图控制器所管理的视图控制器都有一个navigationItem(这里面包含了左按钮,右按钮,中间标题,中间视图)
    //设置导航栏的标题
    navigationItem.title = "Setting"
    
    let leftBarBtn = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: "leftBtnAction")

    //设置右边按钮
    let rightBarBtn = UIBarButtonItem(title: "next", style: UIBarButtonItemStyle.Plain, target: self, action: "rightBtnAction")
    
    //设置导航栏左按钮leftBarButtonItem:(UIBarButtonItem)
    navigationItem.leftBarButtonItem = leftBarBtn
    navigationItem.rightBarButtonItem = rightBarBtn
    // 设置左右item数组
    navigationItem.leftBarButtonItems = [leftBarBtn,rightBarBtn]
    navigationItem.rightBarButtonItems = [leftBarBtn,rightBarBtn]
    //设置中间视图
    let segment = UISegmentedControl(items: ["已接来电","未接来 dian"])
    segment.frame = CGRectMake(0, 0, 100, 30)
    segment.selectedSegmentIndex = 1
    //设置中间视图
    navigationItem.titleView = segment
    
    //导航栏( UINavigationBar)
    //在本类中(视图控制器)访问navigationController就是获取到本视图控制器所在的导航视图控制器
    //设置导航栏是否隐藏
    navigationController?.navigationBarHidden = false
    // 设置导航栏样式
    navigationController?.navigationBar.barStyle = .Default
    //背景颜色 
    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的contentView显示的谁的View?

}

//跳转第二个控制器页面
func rightBtnAction(){
    //(1) 创建第二个控制器
    let secondVC = SecondViewController()
    //(2)使用当前控制器所在的导航视图控制器跳转到第二个控制器pushViewController(进入到下一个页面)
    navigationController?.pushViewController(secondVC, animated: true)
    
    
}

func leftBtnAction(){
    print("click left Btn")
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

import UIKit

class SecondViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    //设置页面颜色为白色
    view.backgroundColor = UIColor.whiteColor()
    
    //设置标题
    navigationItem.title = "SecondVC"
    
    let leftBarBtn = UIBarButtonItem(title: "back", style: UIBarButtonItemStyle.Plain, target: self, action: "backAction:")
    navigationItem.leftBarButtonItem = leftBarBtn
    
    let rightBtn = UIBarButtonItem(title: "进入3", style: UIBarButtonItemStyle.Plain, target: self, action: "pushToThirdVC")
    navigationItem.rightBarButtonItem = rightBtn
    
    
    
}

func pushToThirdVC(){
    let thirdVC = ThirdViewController()
    navigationController?.pushViewController(thirdVC, animated: true)
}


func backAction(btn:UIBarButtonItem){
    print("返回")
    //将SecondVc出棧popViewControllerAnimated:将当前显示在棧顶的控制器出棧(回到上一个页面)
    navigationController?.popViewControllerAnimated(true)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

import UIKit

class ThirdViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.title = "thirdVC"
    let rightBtn = UIBarButtonItem(title: "进入4", style: UIBarButtonItemStyle.Plain, target: self, action: "pushToFourthVC")
    navigationItem.rightBarButtonItem = rightBtn
}

func pushToFourthVC(){
    let fourthVC = FourthViewController()
    navigationController?.pushViewController(fourthVC, animated: true)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

import UIKit

class FourthViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.title = "fourthVC"
    
    let rightBtn = UIBarButtonItem(title: "进入5", style: UIBarButtonItemStyle.Plain, target: self, action: "pushToFitthVC")
    navigationItem.rightBarButtonItem = rightBtn
    
    
    
    
    
}

func pushToFitthVC(){
    let fifthVC = FifthViewController()
    navigationController?.pushViewController(fifthVC, animated: true)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

import UIKit

class FifthViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    
    view.backgroundColor = UIColor.whiteColor()
    navigationItem.title = "FifthVC"
    
    let leftBtn = UIBarButtonItem(title: "backToRoot", style: UIBarButtonItemStyle.Plain, target: self, action: "popToRootViewController")
    navigationItem.leftBarButtonItem = leftBtn
    
    let btn = UIButton(frame: CGRectMake(100,130,80,45))
    btn.setTitle("模态显示", forState: .Normal)
    
    btn.backgroundColor = UIColor.cyanColor()
    btn.addTarget(self, action: "presentToSix", forControlEvents: .TouchUpInside)
    view.addSubview(btn)

}

//点击按钮模态显示第六个视图控制器
func presentToSix(){
    //创建第六个视图控制器
    let sixthVC = SixthViewController()
    //模态显示,跟导航视图控制器没关系 
    //参数completion:模态显示完成之后要执行的闭包
    presentViewController(sixthVC, animated: true) { () -> Void in
        //模态显示动作完成要执行的代码
        print("模态动作已完成")
    }
}

func popToRootViewController(){
    //(1)popToRootViewControllerAnimated:回到根视图控制器

    //        navigationController?.popToRootViewControllerAnimated(true)

// (2)
//先获取到棧里所有的视图控制器
let viewControllers = navigationController?.viewControllers
//获取根视图控制器(因为根视图控制器是最先入棧,所以在第0个下标)
let rootVC:UIViewController = viewControllers![0]

    //导航视图控制器返回到指定的视图控制器
    navigationController?.popToViewController(rootVC, animated: true)
    
    
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

import UIKit

class SixthViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = UIColor.grayColor()
    
    let modelBtn = UIButton(frame: CGRectMake(80,150,100,45))
    modelBtn.setTitle("模态消失", forState: .Normal)
    modelBtn.backgroundColor = UIColor.blueColor()
    modelBtn.addTarget(self, action: "dismissViewcontroller", forControlEvents: .TouchUpInside)
    view.addSubview(modelBtn)
    
    
}

func dismissViewcontroller(){

// (1)第一种方式:模态过程不可定制化 dismissViewcontroller()

//(2)第二种方式:模态消失过程可定制化(需不需要动画,模态结束后执行代码段)
    dismissViewControllerAnimated(true) { () -> Void in
        print("模态消失动作已结束")
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

你可能感兴趣的:(swift导航)