学校学习ios教程笔记,第十节初识ios中的导航栏控制器页面跳转(UINavigationController)和模态

来源:互联网 发布:北京淘宝城 编辑:程序博客网 时间:2024/06/04 19:20

 ios中设置导航栏属性

在做以下代码时必须在AppDelegate将视图控制器改为导航栏视图控制器


import UIKit

class ViewController:UIViewController {

   overridefunc viewDidLoad() {

       super.viewDidLoad()

       //     每一个被导航视图控制所管理的视图控制器都有一个navigationItem(这里面包含了左按钮,右按钮,中间标题,中间视图)

       //       设置导航栏的标题

       navigationItem.title ="Setting"

       //    设置导航栏左按钮(UIBarButtonItem

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

       navigationItem.leftBarButtonItem = leftBarBtn

//        let rightBarBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Search, target: self, action: "rightBtnAction")

       let rightBarBtn =UIBarButtonItem(title:"next", style:UIBarButtonItemStyle.Done, target:self, action:"rightBtnAction")

       navigationItem.rightBarButtonItem = rightBarBtn

        /// let rightBarBtn = UIBarButtonItem[] 设置导航栏按钮数组        

       //设置导航栏的中间视图

       let segment =UISegmentedControl(items: ["1","2"])

        segment.frame=CGRectMake(0,0,100,30)

        segment.selectedSegmentIndex =0

       navigationItem.titleView = segment

        segment.addTarget(self, action:"rightBtnAction", forControlEvents:UIControlEvents.ValueChanged)

       //导航栏(UINavigationBar)在本类中(视图)访问navigationController就是获取到本视图控制器所在的导航视图控制器

       //设置导航栏是否隐藏

          navigationController?.navigationBarHidden =false //true是隐藏

       //设置导航栏的样式

       navigationController?.navigationBar.barStyle = .Black //.Black .Defualt


       //设置导航栏的背景颜色

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


       //设置导航栏本身的颜色

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


      //设置导航栏元素的颜色(例如左按钮,右按钮,中间标题)

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


       //导航栏 半透明 效果0,0)点在左上角  //false (0,0)点在导航栏的左下角


       navigationController?.navigationBar.translucent =true        

       let tempview =UIView(frame:CGRectMake(0,64,150,150))

        tempview.backgroundColor =UIColor.blueColor()      

       view.addSubview(tempview)

    }

   func rightBtnAction(){

      //跳转第二个控制器页面

       //(1)创建第二个控制器

       let secondvc =SecondViewController()

       //(2)使用当前控制器所在的导航视图控制器跳转到第二个控制器push

       navigationController?.pushViewController(secondvc, animated:true)

       print("click right Btn")

    }   

   func leftBtnAction(){       

       print("click left Btn")

    }  

   overridefunc didReceiveMemoryWarning() {

       super.didReceiveMemoryWarning()

       // Dispose of any resources that can be recreated.

    }    

}

页面跳转

UINavigationController通过的方式管理控制器的切换,控制入栈和出栈来展示各个视图控制器。UINavigationController的ContentView里始终显示栈顶控制器的view。viewControllers属性是一个可变数组(NSMutableArray)存储了栈中的所有被管理的控制器,入栈的时候,使用addObject把新的视图控制器对象添加到数组末尾,出栈时removeLastObject移除数组末尾的试图控制器对象。navigationController属性,父类中的属性,每个在栈中的控制器,都能通过此属性,获取自己所在的UINavigationController对象。 


方法名

描述

pushViewController:animated:

进入下一个视图控制器

popViewControllerAnimated:

返回上一个视图控制器

popToViewController:animated:

返回指定的视图控制器

popToRootViewControllerAnimated:

返回根视图控制器


//进入下一个视图控制器

        //(1)创建第二个控制器

        let secondvc =SecondViewController()

        //(2)使用当前控制器所在的导航视图控制器跳转到第二个控制器push

        navigationController?.pushViewController(secondvc, animated:true)

// 返回上一个视图控制器

navigationController?.popViewControllerAnimated(true)

//返回指定视图控制器

    //跳到指定的视图  先拿到栈里所有的视图控制器

       let viewcd = navigationController?.viewControllers

//获取根视图控制器(因为根视图控制器是最先入栈,所以在第0个下标)

      let rootVc: AnyObject = viewcd![0] 

  navigationController?.popToViewController(rootVc as! UIViewController, animated: true)

//返回根视图控制器

navigationController?.popToRootViewControllerAnimated(true)

模态

模态只有两种方式一种就是跳转和返回
--1模态显示

       //创建第六个视图控制器

       let sixvc =SixViewController()

      //模态显示,跟导航视图控制器没有关系

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

           print("模态动作完成")

        }  //completion是一个闭包 模态显示完成之后要执行的闭包


--2模态消失

模态消失过程可定制化(需不需要动画,模态结束后执行代码段)

dismissViewControllerAnimated(true, completion: { () ->Voidin

})    



0 1
原创粉丝点击