在这里插入图片描述

1.创建一个继承自UITabbarViewController控制器,在这个控制器中写如下代码

override func viewDidLoad() {
        super.viewDidLoad()
        //这个按钮就是中间添加按钮
        let middleBtn = UIButton(type: .custom)
        middleBtn.frame = CGRect(x: kScreenWidth/5*2 , y: -10, width: kScreenWidth/5, height: 55)
        middleBtn.setImage(UIImage(named: "tabbar_middle_add"), for: .normal)
        middleBtn.setImage(UIImage(named: "tabbar_middle_add"), for: .highlighted)
        middleBtn.addTarget(self, action: #selector(middleBtnAction), for: .touchUpInside)
        
        let health = HealthViewController()
        let healthNav = NavigationViewController(rootViewController: health)
        healthNav.tabBarItem.image = UIImage(named: "tabbar_health_unselected")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        healthNav.tabBarItem.selectedImage = UIImage(named: "tabbar_health_selected")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        healthNav.title = "健康"
        
        let vedio = VedioViewController()
        let vedioNav = NavigationViewController(rootViewController: vedio)
        vedioNav.tabBarItem.image = UIImage(named: "tabbar_vedio_unselected")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        vedioNav.tabBarItem.selectedImage = UIImage(named: "tabbar_vedio_selected")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        vedio.tabBarItem.imageInsets = UIEdgeInsets(top: -15, left: 0, bottom: 0, right: 0)
        vedioNav.title = "视界"
        
        let middle = UIViewController()//占位用
        middle.tabBarItem.isEnabled = false
        
        let life = LifeViewController()
        let lifeNav = NavigationViewController(rootViewController: life)
        lifeNav.tabBarItem.image = UIImage(named: "tabbar_life_unselected")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        lifeNav.tabBarItem.selectedImage = UIImage(named: "tabbar_life_selected")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        lifeNav.title = "商城"
        
        let mine = MineViewController()
        let mineNav = NavigationViewController(rootViewController: mine)
        mineNav.tabBarItem.image = UIImage(named: "tabbar_mine_unselected")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        mineNav.tabBarItem.selectedImage = UIImage(named: "tabbar_mine_selected")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        mineNav.title = "个人"
        
        viewControllers = [healthNav,vedioNav,middle,lifeNav,mineNav]
        tabBar.tintColor = RGBCOLOR(r: 0, g: 216, b: 160)
        tabBar.barTintColor = UIColor.white
        tabBar.unselectedItemTintColor = RGBCOLOR(r: 191, g: 187, b: 187)
        tabBar.shadowImage = UIImage()
        tabBar.backgroundImage = UIImage(named: "tabbar_bg")
        tabBar.backgroundColor = .white
        tabBar.addSubview(middleBtn)
        tabBar.bringSubviewToFront(middleBtn)
        selectedIndex = 1
        delegate = self
 }

中间按钮点击方法

 @objc func middleBtnAction() {
  }
extension TabBarViewController:UITabBarControllerDelegate {
    //是否可以点击
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        return true
    }
    //点击其他四个item 代理方法
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)   {
      
  }
  }

原文地址:https://blog.csdn.net/weixin_43259805/article/details/128241066

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_24262.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注