導(dǎo)航欄
//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()
//每一個被導(dǎo)航視圖控制器所管理的視圖控制器都有一個navigationItem(這里包含了左按鈕怠李,右按鈕草戈,中間標(biāo)題台颠,中間視圖)
//設(shè)置導(dǎo)航欄標(biāo)題
navigationItem.title = "Setting"
let leftBarBtn = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: "leftBtnAction")
//設(shè)置導(dǎo)航欄左按鈕(UIBarButtonItem)
navigationItem.leftBarButtonItem = leftBarBtn
let rightBarBtn = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "rightBtnAction")
navigationItem.rightBarButtonItem = rightBarBtn
//? ? ? ? navigationItem.leftBarButtonItems = [leftBarBtn,rightBarBtn]
//? ? ? ? navigationItem.rightBarButtonItems = [leftBarBtn,rightBarBtn]
//設(shè)置中間視圖
let segment = UISegmentedControl(items: ["已接來電","未接來電"])
segment.frame = CGRectMake(0, 0, 100, 30)
segment.selectedSegmentIndex = 0
navigationItem.titleView = segment
//導(dǎo)航欄(UINavigationBar)
//在本類中(視圖控制器)訪問navigationController就是獲取的到本視圖控制器所在的導(dǎo)航視圖控制器
//設(shè)置導(dǎo)航欄是否隱藏
navigationController?.navigationBarHidden = false
//設(shè)置導(dǎo)航欄的樣式
navigationController?.navigationBar.backgroundColor = UIColor.cyanColor()
//設(shè)置導(dǎo)航欄本身的顏色
navigationController?.navigationBar.barTintColor = UIColor.yellowColor()
//設(shè)置導(dǎo)航欄元素的顏色
navigationController?.navigationBar.tintColor = UIColor.redColor()
//導(dǎo)航欄半透明效果
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")
}
//跳轉(zhuǎn)到第二個控制器的頁面
func rightBtnAction(){
//(1)創(chuàng)建第二個控制器
let secondVC = secondViewController()
//(2)使用當(dāng)前控制器所在的導(dǎo)航視圖控制器跳轉(zhuǎn)到第二個控制器,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"
//設(shè)置顏色
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:將當(dāng)前的顯示在棧頂?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)顯示定嗓,跟導(dǎo)航視圖控制器沒關(guān)系
//參數(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個下標(biāo))
let rootVC:UIViewController = viewControllers![0]
//導(dǎo)航視圖控制器返回到指定的視圖控制器
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)結(jié)束后執(zhí)行代碼段)
dismissViewControllerAnimated(true) { () -> Void in
print("模態(tài)消失動作已結(jié)束")
}
}
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.
}
*/
}
/*
}
*/
}
/*
頁面跳轉(zhuǎn)的方式
1.模態(tài)顯示
//創(chuàng)建第六個視圖控制器
letsixthVC = SixthViewController()
//模態(tài)顯示,跟導(dǎo)航視圖控制器沒關(guān)系
//參數(shù)completion:模態(tài)顯示完成之后要執(zhí)行的閉包
presentViewController(sixthVC, animated:true) { () ->Voidin
//模態(tài)顯示動作完成要執(zhí)行的代碼
print("模態(tài)動作已完成")
}
2.push
*/