Swift基础之神奇的导航条

先来介绍一下导航条的基础知识:

一、UINavgationController-导航控制器

我们先来看一段简单的代码:

  //在AppDelegate中创建导航控制器,并设置它为根视图
   let root:RootViewController = RootViewController()
   let nav = UINavigationController(rootViewController: root)
   self.window!.rootViewController = nav
   //在RootViewController中设置标题(title)和item
   self.title = "第一页"
   let nextItem = UIBarButtonItem(title: "下一页", style:.Plain, target: self, action: #selector(RootViewController.nextPage))
   self.navigationItem.rightBarButtonItem = nextItem

   //方法实现
   func nextPage()
        //推出下一页
        let vc = ViewController()
        svc.delegate = self
        self.navigationController!.pushViewController(vc, animated: true)
   }

在这段简单的代码中,主要实现了设置导航控制器为根视图,并实现推出下一页的功能。当然我们还可以对导航控制器的导航条UINavgationBar进行属性设置:

   //设置navgationBar的颜色
   nav.navigationBar.barTintColor = UIColor.clearColor()
   //关闭navigationBar的毛玻璃效果 
   /***在设置背景图片的时候会出现与图片颜色不对的情况,只要将translucent设置成false就OK啦***/
   nav.navigationBar.translucent = false
关于navgationBar的图片设置简单说明一下:
  • 不同尺寸的图片效果不同:
    1.320 * 44,只会给navigationBar附上图片
    2.高度小于44,以及大于44且小于64:会平铺navigationBar以及状态条上显示
    3.高度等于64:整个图片在navigationBar以及状态条上显示

     nav.navigationBar.setBackgroundImage(UIImage(named: "background"), forBarMetrics:.Compact)
    //它有四种状态Default,Compact,CompactPrompt,DefaultPrompt,大家可以根据自己的需求来修改
    

常用的也就是这些,如有不足大家可以在评论上说明,大脸猫会及时添加的哦。

二、push和pop

在前面那段简单的代码中小伙伴们已经看到了,大脸猫用到了push,实现了从RootViewController到ViewController的跳转。下面我们来详细的介绍一下push和pop的用法。

push:

  self.navigationController?.pushViewController(viewController, animated: true)
我先来介绍一个每个参数:
  • 参数1:你要push到的控制器
  • 参数2:是否活动,这个参数一般情况下都是true

pop:

  //返回到上一个页面
  self.navigationController?.popViewControllerAnimated(animated: true)
  //返回到指定页面
  self.navigationController?.popToViewController(root, animated: true)
  //返回到根视图控制器
  self.navigationController?.popToRootViewControllerAnimated(true)

三、UIBarButtonItem

细心的小伙伴们应该会发现,在上面那段代码中,我已经使用过一次UIBarButtonItem了,代码是这个样子的:

   let nextItem = UIBarButtonItem(title: "下一页", style:.Plain, target: self, action: #selector(RootViewController.nextPage))
   self.navigationItem.rightBarButtonItem = nextItem

这两行代码实现了UIBarButtonItem的创建和事件的添加,还有就是设置它右边的item。
我们再来看看其他的创建方式:

    //带文字
    let barBtn = UIBarButtonItem(title: "next", style: .Done, target: self, action: #selector(RootViewController.nextPage))
    //带背景图片
    let barBtn = UIBarButtonItem(image: UIImage(named: "background"), style: .Done, target: self, action: #selector(RootViewController.nextPage))
    //带背景图片和手机风景图片
    let barBtn = UIBarButtonItem(image: UIImage(named: "background"), landscapeImagePhone: UIImage(named: "background_1"), style: .Done, target: self, action: #selector(RootViewController.nextPage))
    //设置位置在左边还是在右边
    self.navigationItem.rightBarButtonItem = barBtn
    self.navigationItem.leftBarButtonItem = barBtn
    //自定义视图
    let barButton = UIBarButtonItem(customView: button)

四、UINavgationController的代理方法

导航条也是有代理的哦,但是用的不是很多,我们来了解一下

    self.navigationController?.delegate = self;//设置代理
    //代理方法
    func navigationController(navigationController: UINavigationController,       willShowViewController viewController: UIViewController, animated: Bool) {

    }
    func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {

    }

简单的基础知识就先介绍到这里,后面有时间还会发布有关导航条侧滑的实现和导航条的其他功能,( _ )/~~拜拜

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容