導航欄,頁面跳轉

導航欄

//ViewController.swift

//UINavigationController

//

//Created by lanou on 16/11/2.

//Copyright (c) 2016年lanou. All rights reserved.

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {

super.viewDidLoad()

//每一個被導航視圖控制器所管理的視圖控制器都有一個navigationItem(這里包含了左按鈕,右按鈕,中間標題,中間視圖)

//設置導航欄標題

navigationItem.title = "Setting"

let leftBarBtn = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: "leftBtnAction")

//設置導航欄左按鈕(UIBarButtonItem)

navigationItem.leftBarButtonItem = leftBarBtn

let rightBarBtn = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "rightBtnAction")

navigationItem.rightBarButtonItem = rightBarBtn

//? ? ? ? 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

//設置導航欄的樣式

navigationController?.navigationBar.backgroundColor = UIColor.cyanColor()

//設置導航欄本身的顏色

navigationController?.navigationBar.barTintColor = UIColor.yellowColor()

//設置導航欄元素的顏色

navigationController?.navigationBar.tintColor = UIColor.redColor()

//導航欄半透明效果

navigationController?.navigationBar.translucent = true

let myView = UIView(frame: CGRectMake(0, 0, 60, 60))

myView.backgroundColor = UIColor.blueColor()

view.addSubview(myView)

}

func leftBtnAction(){

print("click left Btn")

}

//跳轉到第二個控制器的頁面

func rightBtnAction(){

//(1)創(chuàng)建第二個控制器

let secondVC = secondViewController()

//(2)使用當前控制器所在的導航視圖控制器跳轉到第二個控制器,pushViewController可以進入到下一個頁面

navigationController?.pushViewController(secondVC, animated: true)

print("click right Btn")

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

//secondViewController.swift

//UINavigationController

//

//Created by lanou on 16/11/3.

//Copyright (c) 2016年lanou. All rights reserved.

import UIKit

class secondViewController: UIViewController {

override func viewDidLoad() {

super.viewDidLoad()

navigationItem.title = "seconrVC"

//設置顏色

view.backgroundColor = UIColor.whiteColor()

let leftBarBtn = UIBarButtonItem(title: "back", style: UIBarButtonItemStyle.Plain, target: self, action: "backAction:")

navigationItem.leftBarButtonItem = leftBarBtn

}

func backAction(btn:UIBarButtonItem){

println("返回")

//將secondVC出棧,popViewControllerAnimated:將當前的顯示在棧頂?shù)目刂破鞒鰲?/p>

navigationController?.popViewControllerAnimated(true)

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

}

}


//? FifthViewController.swift

//? UINavigationController

//

//? Created by SZT on 2016/11/3.

//? Copyright ? 2016年 SZT. All rights reserved.

//

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("模態(tài)顯示", forState: .Normal)

btn.backgroundColor = UIColor.cyanColor()

btn.addTarget(self, action: "presentToSix", forControlEvents: .TouchUpInside)

view.addSubview(btn)

}

//點擊按鈕模態(tài)顯示第六個視圖控制器

func presentToSix(){

//創(chuàng)建第六個視圖控制器

let sixthVC = SixthViewController()

//模態(tài)顯示,跟導航視圖控制器沒關系

//參數(shù)completion:模態(tài)顯示完成之后要執(zhí)行的閉包

presentViewController(sixthVC, animated: true) { () -> Void in

//模態(tài)顯示動作完成要執(zhí)行的代碼

print("模態(tài)動作已完成")

}

}

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.

}

}

//SixthViewController.swift

//UINavigationController

//

//Created by SZT on 2016/11/3.

//Copyright ? 2016年SZT. All rights reserved.

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("模態(tài)消失", forState: .Normal)

modelBtn.backgroundColor = UIColor.blueColor()

modelBtn.addTarget(self, action: "dismissViewcontroller", forControlEvents: .TouchUpInside)

view.addSubview(modelBtn)

}

func dismissViewcontroller(){

//? ? (1)第一種方式:模態(tài)過程不可定制化? dismissViewcontroller()

//(2)第二種方式:模態(tài)消失過程可定制化(需不需要動畫,模態(tài)結束后執(zhí)行代碼段)

dismissViewControllerAnimated(true) { () -> Void in

print("模態(tài)消失動作已結束")

}

}

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.

}

*/

}

/*

}

*/

}

/*

頁面跳轉的方式

1.模態(tài)顯示

//創(chuàng)建第六個視圖控制器

letsixthVC = SixthViewController()

//模態(tài)顯示,跟導航視圖控制器沒關系

//參數(shù)completion:模態(tài)顯示完成之后要執(zhí)行的閉包

presentViewController(sixthVC, animated:true) { () ->Voidin

//模態(tài)顯示動作完成要執(zhí)行的代碼

print("模態(tài)動作已完成")

}

2.push

*/

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容