//自定义TabBar
class Seadra_JJBLP_CustomTabBar: UITabBar {
// index:凸起按钮下标,protrudingHeight:凸起高度
typealias Seadra_JJBLP_TabBarItemInfo= (topic:String, imageName:String, selectedImageName:String,index:Int,protrudingHeight:CGFloat)
var seadra_property_itemInfo : Seadra_JJBLP_TabBarItemInfo!
var seadra_property_tapBut = UIButton.init(type: .custom)
var seadra_property_tabBarBlock : () -> Void = { }
//初始化
init(itemInfo:Seadra_JJBLP_TabBarItemInfo,clickBlock:@escaping() ->Void) {
super.init(frame: CGRect.init(x: 0, y: SCREEN_HEIGHT-kTabBarHeight, width: SCREEN_WIDTH, height: kTabBarHeight))
seadra_property_itemInfo = itemInfo
seadra_property_tabBarBlock = clickBlock
}
// 重写
override func layoutSubviews() {
super.layoutSubviews()
forsubinself.subviews{//避免重复创建
if sub.isKind(of:UIControl.self) {
let view = sub
if view.tag==220{
return
// view.removeFromSuperview()
}
}
}
for but in self.subviews{
if but.isKind(of:NSClassFromString("UITabBarButton")!) {
seadra_property_tabBarButArr.append(but)
}
}
// 私有类型UITabBarButton转为UIView
let button : UIView = seadra_property_tabBarButArr[seadra_property_itemInfo.index] as! UIView
let bgView =UIControl.init(frame:CGRect.init(x: button.frame.minX, y:-seadra_property_itemInfo.protrudingHeight, width: button.frame.width, height: button.frame.width))
bgView.tag=220
bgView.layer.cornerRadius = bgView.frame.width*0.5
bgView.layer.masksToBounds=true
bgView.backgroundColor = UITabBar.appearance().barTintColor//背景颜色保持和设置的系统tabbar一致
bgView.addTarget(self, action:#selector(seadra_func_click), for: .touchUpInside)
self.addSubview(bgView)
// 设置tabbar按钮图片
seadra_property_tapBut.setImage(UIImage.init(named: seadra_property_itemInfo.imageName), for: .normal)
seadra_property_tapBut.setImage(UIImage.init(named:seadra_property_itemInfo.selectedImageName), for: .selected)
seadra_property_tapBut.frame=CGRect.init(x:0, y:0, width: bgView.frame.width, height: bgView.frame.height)
bgView.addSubview(seadra_property_tapBut)
seadra_property_tapBut.setTitle(seadra_property_itemInfo.topic, for: .normal)
// seadra_property_tapBut.titleLabel?.font = UIFont.systemFont(ofSize: 10,weight: .medium)//字体调整和系统一致
seadra_property_tapBut.titleLabel?.font = UIFont.systemFont(ofSize: 14,weight: .medium)
seadra_property_tapBut.setTitleColor(Seadra_JJBLP_BaseColor_999999, for: .normal)
seadra_property_tapBut.setTitleColor(Seadra_JJBLP_BaseColor, for: .selected)
// 调整图片位置居中
seadra_property_tapBut.imageEdgeInsets = UIEdgeInsets.init(top: -10, left: CGFloat(seadra_property_tapBut.frame.width*0.5-(seadra_property_tapBut.imageView?.frame.width)!*0.5-(seadra_property_tapBut.imageView?.frame.minX)!), bottom: 10, right: -CGFloat(seadra_property_tapBut.frame.width*0.5-(seadra_property_tapBut.imageView?.frame.width)!*0.5-(seadra_property_tapBut.imageView?.frame.minX)!))
// 调整标题位置居中
seadra_property_tapBut.titleEdgeInsets = UIEdgeInsets.init(top: (seadra_property_tapBut.imageView?.frame.height)!, left: -CGFloat((seadra_property_tapBut.titleLabel?.frame.minX)!-seadra_property_tapBut.frame.width*0.5+(seadra_property_tapBut.titleLabel?.frame.width)!*0.5), bottom: -(seadra_property_tapBut.imageView?.frame.height)!*0.5, right: CGFloat((seadra_property_tapBut.titleLabel?.frame.minX)!-seadra_property_tapBut.frame.width*0.5+(seadra_property_tapBut.titleLabel?.frame.width)!*0.5))
}
@objc func seadra_func_click() {
seadra_property_tabBarBlock()//
seadra_property_tapBut.isSelected = true//点击后选中状态
}
// 扩展点击范围
overridefunchitTest(_point:CGPoint, with event:UIEvent?) ->UIView? {
if self.isHidden==false{
let bgView =self.viewWithTag(220)
if((bgView?.frame.contains(point))==true) {
returnbgView
}
}
return super.hitTest(point, with: event)
}
自定义TabBarController中使用
// 重写代理方法
override functabBar(_tabBar:UITabBar, didSelect item:UITabBarItem) {
// 点击其它tabbarbutton走这个代理,在这里面把凸起按钮改成未选中状态
self.mainTabBar.seadra_property_tapBut.isSelected = false
}
mainTabBar = Seadra_JJBLP_CustomTabBar.init(itemInfo: ("分类","Seadra_JJBLP_tab_hero_default","Seadra_JJBLP_tab_hero_selected",self.outIndex,30), clickBlock: {//点击凸起按钮,选中对应下标界面
self.selectedIndex=self.outIndex
})
self.setValue(mainTabBar, forKey:"tabBar")//替换系统tabbar