iOS 凸起tabBarButton


//自定義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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末歇攻,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子赵哲,更是在濱河造成了極大的恐慌滞项,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件睛蛛,死亡現(xiàn)場離奇詭異鹦马,居然都是意外死亡胧谈,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進店門荸频,熙熙樓的掌柜王于貴愁眉苦臉地迎上來菱肖,“玉大人,你說我怎么就攤上這事旭从∥惹浚” “怎么了?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵和悦,是天一觀的道長退疫。 經(jīng)常有香客問我,道長鸽素,這世上最難降的妖魔是什么褒繁? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮馍忽,結(jié)果婚禮上棒坏,老公的妹妹穿的比我還像新娘。我一直安慰自己遭笋,他們只是感情好坝冕,可當我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著瓦呼,像睡著了一般喂窟。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上吵血,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天谎替,我揣著相機與錄音,去河邊找鬼蹋辅。 笑死,一個胖子當著我的面吹牛挫掏,可吹牛的內(nèi)容都是我干的侦另。 我是一名探鬼主播,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼尉共,長吁一口氣:“原來是場噩夢啊……” “哼褒傅!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起袄友,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤殿托,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后剧蚣,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體支竹,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡旋廷,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了礼搁。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片饶碘。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖馒吴,靈堂內(nèi)的尸體忽然破棺而出扎运,到底是詐尸還是另有隱情,我是刑警寧澤饮戳,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布豪治,位于F島的核電站,受9級特大地震影響扯罐,放射性物質(zhì)發(fā)生泄漏鬼吵。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一篮赢、第九天 我趴在偏房一處隱蔽的房頂上張望齿椅。 院中可真熱鬧,春花似錦启泣、人聲如沸涣脚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽遣蚀。三九已至,卻和暖如春纱耻,著一層夾襖步出監(jiān)牢的瞬間芭梯,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工弄喘, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留玖喘,地道東北人。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓蘑志,卻偏偏與公主長得像累奈,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子急但,可洞房花燭夜當晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容