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) {
}
}