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.
}
*/

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 156,423评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,339评论 1 289
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,241评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,503评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,824评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,262评论 1 207
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,615评论 2 309
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,337评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,989评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,300评论 2 240
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,829评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,193评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,753评论 3 230
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,970评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,708评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,295评论 2 267
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,207评论 2 258

推荐阅读更多精彩内容