//自定義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{//避免重復創(chuàng)建
? ? ? ? ? ? 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轉(zhuǎn)為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//背景顏色保持和設置的系統(tǒng)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)//字體調(diào)整和系統(tǒng)一致
? ? ? ? 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)
//? ? ? ? 調(diào)整圖片位置居中
? ? ? ? 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)!))
//? ? ? ? 調(diào)整標題位置居中
? ? ? ? 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//點擊后選中狀態(tài)
? ? }
//? ? 擴展點擊范圍
? ? 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走這個代理,在這里面把凸起按鈕改成未選中狀態(tài)
? ? ? ? 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")//替換系統(tǒng)tabbar