当前位置:   article > 正文

swift之navigationController、navigation bar_swift basenavigationcontroller

swift basenavigationcontroller

 // 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:

  1. import UIKit
  2. class LYBBaseNav: UINavigationController {
  3. override func viewDidLoad() {
  4. super.viewDidLoad()
  5. }
  6. func configBackBtn() -> [UIBarButtonItem] {
  7. // 返回按钮
  8. let backButton = UIButton(type: .custom)
  9. // 给按钮设置返回箭头图片
  10. backButton.setImage(UIImage(named: "fanhui"), for: .normal)
  11. // 设置frame
  12. backButton.frame = CGRect(x: 200, y: 13, width: 18, height: 18)
  13. backButton.addTarget(self, action: #selector(back), for: .touchUpInside)
  14. // 自定义导航栏的UIBarButtonItem类型的按钮
  15. let backView = UIBarButtonItem(customView: backButton)
  16. // 重要方法,用来调整自定义返回view距离左边的距离
  17. let barButtonItem = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
  18. barButtonItem.width = -5
  19. // 返回按钮设置成功
  20. return [barButtonItem, backView]
  21. }
  22. @objc func back(){
  23. self.popViewController(animated: true)
  24. }
  25. //拦截跳转事件,每一次push都会调用
  26. override func pushViewController(_ viewController: UIViewController, animated: Bool) {
  27. if self.children.count > 0 {
  28. viewController.hidesBottomBarWhenPushed = true
  29. //添加返回按钮方式一
  30. viewController.navigationItem.leftBarButtonItems=self.configBackBtn()
  31. }
  32. super.pushViewController(viewController, animated: animated)
  33. }
  34. //返回上一级界面之前的设置操作
  35. override func popViewController(animated: Bool) -> UIViewController? {
  36. print("返回");
  37. //每一次对viewController进行push的时候,会把viewController放入一个栈中
  38. //每一次对viewController进行pop的时候,会把viewController从栈中移除
  39. if self.children.count == 2
  40. {
  41. //如果viewController栈中存在的ViewController的个数为两个,再返回上一级界面就是根界面了
  42. //那么要对tabbar进行显示
  43. let controller:UIViewController = self.children[0]
  44. controller.hidesBottomBarWhenPushed = false
  45. }
  46. else
  47. {
  48. //如果viewController栈中存在的ViewController的个数超过两个,对要返回到的上一级的界面设置hidesBottomBarWhenPushed = true
  49. //把tabbar进行隐藏
  50. let count = self.children.count-2
  51. let controller = self.children[count]
  52. controller.hidesBottomBarWhenPushed = true
  53. }
  54. return super.popViewController(animated: false)
  55. }
  56. }

基础控制器VC:

  1. import UIKit
  2. class LYBBaseVC: UIViewController {
  3. override func viewDidLoad() {
  4. super.viewDidLoad()
  5. // String.ReachabilitymonitorNet()
  6. // String.ReachabilityOncemonitorNet()
  7. self.edgesForExtendedLayout = UIRectEdge.init(rawValue: 0)
  8. self.view.backgroundColor=UIColor.white
  9. //修改导航栏标题文字颜色
  10. // self.navigationController?.navigationBar.titleTextAttributes =
  11. // [.foregroundColor: UIColor.red,.font:UIFont.systemFont(ofSize: 12)]
  12. }
  13. override func viewWillAppear(_ animated: Bool) {
  14. super.viewWillAppear(animated)
  15. //设置导航栏背景透明
  16. //self.navigationController?.navigationBar.setBackgroundImage(UIImage(),for: .default)
  17. //self.navigationController?.navigationBar.shadowImage = UIImage()
  18. }
  19. //视图将要消失
  20. override func viewWillDisappear(_ animated: Bool) {
  21. super.viewWillDisappear(animated)
  22. //重置导航栏背景
  23. // self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
  24. // self.navigationController?.navigationBar.shadowImage = nil
  25. }
  26. }

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/219349
推荐阅读
  

闽ICP备14008679号