博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
swift之navigationController、navigation bar
阅读量:4290 次
发布时间:2019-05-27

本文共 4892 字,大约阅读时间需要 16 分钟。

 // 1.设置导航栏标题属性:设置标题颜色

        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor.red]  

// 2.设置导航栏前景色:设置item指示色

        self.navigationController?.navigationBar.tintColor = UIColor.purple

             

// 3.设置导航栏半透明

        self.navigationController?.navigationBar.isTranslucent = true

           

// 4.设置导航栏背景图片

        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)//设置导航栏透明,只有当 self.navigationController?.navigationBar.isTranslucent = true时才有效果

self.navigationController?.navigationBar.setBackgroundImage(UIImage.init(named: "bannerhomeOne"), for: UIBarMetrics.default)//设置有图片背景的导航

       

// 5.设置导航栏阴影图片----导航栏底部的那一条黑线

        self.navigationController?.navigationBar.shadowImage = UIImage()

 

 //6.设置导航栏的背景色----如果设置了背景图片,这个就不起作用了

self.navigationController?.navigationBar.barTintColor=UIColor.init(red:39.0/255, green:131.0/255, blue:207.0/255, alpha:1)

 

导航上右侧按钮:

  func addrightbtn(){

        //navitem search 搜索按钮

            let buttonSearch = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: 20, height: 20))

        buttonSearch.setTitle("设置", for: UIControl.State.normal)

        buttonSearch.addTarget(self, action: #selector(settingbtn), for: UIControl.Event.touchUpInside)

            let barButtonSearch = UIBarButtonItem(customView: buttonSearch)

            //调节按钮位置

        let spacer = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonItem.SystemItem.fixedSpace, target: nil, action: nil)

            spacer.width = -5;

            

            //设置按钮

            self.navigationItem.rightBarButtonItems = [spacer, barButtonSearch]

    }

    @objc func settingbtn(){

        print("设置")

        let mysettingvc = LYBMysettingvc.init()

        navigationController?.pushViewController(mysettingvc, animated: true)

    }

基础导航控制器NAV:

import UIKitclass LYBBaseNav: UINavigationController {    override func viewDidLoad() {        super.viewDidLoad()    }        func configBackBtn() -> [UIBarButtonItem] {         // 返回按钮         let backButton = UIButton(type: .custom)         // 给按钮设置返回箭头图片         backButton.setImage(UIImage(named: "fanhui"), for: .normal)         // 设置frame         backButton.frame = CGRect(x: 200, y: 13, width: 18, height: 18)         backButton.addTarget(self, action: #selector(back), for: .touchUpInside)         // 自定义导航栏的UIBarButtonItem类型的按钮         let backView = UIBarButtonItem(customView: backButton)         // 重要方法,用来调整自定义返回view距离左边的距离         let barButtonItem = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)         barButtonItem.width = -5         // 返回按钮设置成功        return  [barButtonItem, backView]     }        @objc func back(){        self.popViewController(animated: true)    }        //拦截跳转事件,每一次push都会调用    override func pushViewController(_ viewController: UIViewController, animated: Bool) {    if self.children.count > 0 {        viewController.hidesBottomBarWhenPushed = true        //添加返回按钮方式一        viewController.navigationItem.leftBarButtonItems=self.configBackBtn()            }    super.pushViewController(viewController, animated: animated)    }    //返回上一级界面之前的设置操作  override func popViewController(animated: Bool) -> UIViewController? {    print("返回");      //每一次对viewController进行push的时候,会把viewController放入一个栈中      //每一次对viewController进行pop的时候,会把viewController从栈中移除      if self.children.count == 2      {          //如果viewController栈中存在的ViewController的个数为两个,再返回上一级界面就是根界面了          //那么要对tabbar进行显示          let controller:UIViewController = self.children[0]          controller.hidesBottomBarWhenPushed = false                }      else      {          //如果viewController栈中存在的ViewController的个数超过两个,对要返回到的上一级的界面设置hidesBottomBarWhenPushed = true          //把tabbar进行隐藏          let count = self.children.count-2          let controller = self.children[count]          controller.hidesBottomBarWhenPushed = true      }                  return super.popViewController(animated: false)  }}

基础控制器VC:

import UIKitclass LYBBaseVC: UIViewController {    override func viewDidLoad() {        super.viewDidLoad()//        String.ReachabilitymonitorNet()//        String.ReachabilityOncemonitorNet()        self.edgesForExtendedLayout = UIRectEdge.init(rawValue: 0)        self.view.backgroundColor=UIColor.white               //修改导航栏标题文字颜色//               self.navigationController?.navigationBar.titleTextAttributes =//                [.foregroundColor: UIColor.red,.font:UIFont.systemFont(ofSize: 12)]    }        override func viewWillAppear(_ animated: Bool) {        super.viewWillAppear(animated)                //设置导航栏背景透明//self.navigationController?.navigationBar.setBackgroundImage(UIImage(),for: .default)//self.navigationController?.navigationBar.shadowImage = UIImage()    }        //视图将要消失       override func viewWillDisappear(_ animated: Bool) {           super.viewWillDisappear(animated)                    //重置导航栏背景//           self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)//           self.navigationController?.navigationBar.shadowImage = nil       }       }

 

 

转载地址:http://cimgi.baihongyu.com/

你可能感兴趣的文章
stl 中 set容器用法
查看>>
有序数组求交集
查看>>
文字常量区与栈
查看>>
非阻塞connect 编写方法
查看>>
epoll 边沿触发
查看>>
String类 默认生成的函数
查看>>
Linux 软连接与硬链接
查看>>
视音频数据处理入门:H.264视频码流解析
查看>>
视音频数据处理入门:AAC音频码流解析
查看>>
视音频数据处理入门:UDP-RTP协议解析
查看>>
视音频数据处理入门:FLV封装格式解析
查看>>
最简单的基于FFMPEG的封装格式转换器(无编解码)
查看>>
base64 编码原理
查看>>
单链表是否有环的问题
查看>>
判断两个链表是否相交并找出交点
查看>>
归并排序
查看>>
STL常见问题
查看>>
time_wait和close_wait状态
查看>>
STL中vector、list、deque和map的区别
查看>>
Linux下多线程查看工具(pstree、ps、pstack)
查看>>