UINavigationController及顶部导航条

  1. UINavigationController管理一个VC的栈,栈底的VC叫做这个UINavigationController的root view controller.
  2. 有一个函数叫做popToRootViewController(animated:),就是返回rootVC。
  3. UINavigationController在顶部有一个navigationBar,继承自UIView。它的frame,bound及alpha值都不能改变。隐藏或者显示这个navigationBar要调用UINavigationController的setNavigationBarHidden(_:animated:)方法。其中动画的时间是一个常量UINavigationControllerHideShowBarDuration,不能改变。

    To show or hide the navigation bar, you should always do so through the navigation controller by changing its isNavigationBarHidden property or calling the setNavigationBarHidden(_:animated:) method.

                self.navigationController?.setNavigationBarHidden(false, animated: true)
                self.navigationController?.navigationBarHidden
    
    
  4. navigationBar的各个属性
    NavBar的各个属性

  5. navigationBar的背景色

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

    背景色

  6. navigationBar的配置
    navigationBar有左、中、右三个部分。显示的具体内容是根据当前VC的UINavigationItem属性及栈中前一个VC的UINavigationItem属性配置的。

  7. UINavigationItem
    UINavigationItem作为VC的属性,在这个VC处于navigation stack时,用来设置定于导航条的样式等等。属性分别用来设置导航条左、中、右的样式等。

    • 左边

      • leftBarButtonItem 最左边的按钮
      • leftBarButtonItems 最左边的按钮们(可以有多个)
      • setLeftBarButtonItems(_:animated:)
      self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "left", style: .Plain, target: self, action: #selector(TGVCButtom.leftButtonClicked(_:)))
      self.navigationItem.leftBarButtonItem?.tintColor = UIColor.brownColor()
      let dict = [NSForegroundColorAttributeName : UIColor.blueColor(),
                  NSKernAttributeName: NSNumber.init(int: 10) 
      self.navigationItem.leftBarButtonItem?.setTitleTextAttributes(dict, forState: .Normal)
      
      let sencondleftBarButtonItem = UIBarButtonItem.init(title: "leftSecond", style: .Plain, target: self, action: #selector(TGVCButtom.secondLeftButtonClicked(_:)))  
      self.navigationItem.setLeftBarButtonItems([self.navigationItem.leftBarButtonItem!,sencondleftBarButtonItem], animated: true)
      

      左边的按钮们

    • 中间

      • title

        self.navigationItem.title = "buttomVC"
        
      • titleView
        会覆盖title。如果需要设置button、富文本可以考虑这个

        Custom title views are centered on the navigation bar and may be resized to fit

        self.navigationItem.titleView = UIImageView.init(image: TGResizeImage(UIImage.init(named: "cat"), byRatio: 0.5) )
        
    • 右边。和左边类似。
      • rightBarButtonItem
      • rightBarButtonItems
      • setRightBarButtonItems(_:animated:)
    • 返回按钮

      有时候既要有一个返回的按钮,又要有一个左边的按钮。达到微信的网页常有的返回+关闭的效果。

      返回+关闭

      假设一个当前VC是A,push一个VC出去。

      self.navigationController?.pushViewController(self.topVC, animated:true)
      

      那么需要

      1. 设置自己的backBarButtonItem

        self.navigationItem.backBarButtonItem = UIBarButtonItem.init(title: "back", style: .Plain, target: nil, action: nil);
        
      2. 设置topVC的leftBarButtonItem

        self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "leftButton", style: .Done, target: self, action: nil)
        
      3. 设置topVC的两个变量

        self.navigationItem.hidesBackButton = false //返回按钮没有被隐藏
        self.navigationItem.leftItemsSupplementBackButton = true //返回按钮可以和左边的按钮一起出现
        

        返回+关闭

  8. 奇怪的prompt

    A single line of text displayed at the top of the navigation bar

        self.navigationItem.prompt = "promt"//不知道干嘛的。。有啥用呢
    

    奇怪的prompt

  9. 一个解释

  10. 如何禁止左滑返回

    self.navigationController?.interactivePopGestureRecognizer?.enabled = false
    

posted on 2017-01-14 08:51  花老🐯  阅读(237)  评论(0编辑  收藏  举报

导航