Swift中uitabbar選中背景圖適配(selectionIndicatorImage)

選中的背景是圖片類型肤寝,我的項目是一個顏色漸變的圖片育苟,正常使用時切三套圖可以,可是橫屏后醋闭,圖片就不夠大了礼搁,還有ipad等。

上代碼:

overridefuncviewDidLoad() {

? ? ? ? super.viewDidLoad()


//? ? ? ? self.tabBar.clipsToBounds = true

//? ? ? ? self.tabBar.selectionIndicatorImage = UtilitiesTools.shared().tabSelectionImage(size: CGSize.init(width: screenWidth, height: 49))


? ? ? ? print(screenWidth,self.tabBar.bounds.size.height)

? ? ? ? UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.init(red: 1/255.0, green: 142/255.0, blue: 200/255.0, alpha: 1)], for: .normal)

? ? ? ? UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.white], for: .selected)

? ? ? ? tabLine1.image=? imageLiteral(resourceName: "img_tab_parting")

? ? ? ? tabLine2.image=? imageLiteral(resourceName: "img_tab_parting")

? ? ? ? self.tabBar.addSubview(tabLine1)

? ? ? ? self.tabBar.addSubview(tabLine2)


? ? ? ? tabSelectIma.image=UtilitiesTools.shared().tabSelectionImage(size:CGSize.init(width:screenWidth, height:49))

? ? ? ? tabSelectIma.tag = 100

? ? ? ? self.tabBar.addSubview(tabSelectIma)

? ? ? ? self.tabBar.sendSubview(toBack:tabSelectIma)

? ? ? ? self.setMyTabButton()

? ? ? ? //隱藏原生UITabBarButton

? ? ? ? fortabBarButton:UIViewinself.tabBar.subviews{

? ? ? ? ? ? iftabBarButton.isKind(of:NSClassFromString("UITabBarButton")!) {

? ? ? ? ? ? ? ? tabBarButton.alpha=0

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? // Do any additional setup after loading the view.

? ? }

//畫tabbarButton

? ? funcsetMyTabButton() {

? ? ? ? 3.timesNum{ (time)in

? ? ? ? ? ? lettabButton =UIButton.init(frame:CGRect.init(x:screenWidth/3*CGFloat.init(time-1), y:0, width:screenWidth/3, height:49))

? ? ? ? ? ? iftime ==1{

? ? ? ? ? ? ? ? tabButton.setImage( imageLiteral(resourceName: "Jurisdiction_customer_pressed"), for:UIControlState.normal)

? ? ? ? ? ? }else{

? ? ? ? ? ? ? ? tabButton.setImage([ imageLiteral(resourceName: "Jurisdiction_customer_normal"), imageLiteral(resourceName: "Visit_rankinge_normal"), imageLiteral(resourceName: "Sales_architecture_normal")][time-1], for:UIControlState.normal)

? ? ? ? ? ? ? ? tabButton.setTitleColor(UIColor.init(red:1/255.0, green:142/255.0, blue:200/255.0, alpha:1), for:UIControlState.normal)

? ? ? ? ? ? }

? ? ? ? ? ? tabButton.imageEdgeInsets.left=-10

? ? ? ? ? ? tabButton.titleLabel?.font=UIFont.boldSystemFont(ofSize:15)

? ? ? ? ? ? tabButton.setTitle(["轄區(qū)客戶","拜訪排名","銷售架構(gòu)"][time-1], for:UIControlState.normal)

? ? ? ? ? ? tabButton.tag=110+time-1

? ? ? ? ? ? tabButton.addTarget(self, action:#selector(tabbarSelect(sender:)), for:UIControlEvents.touchUpInside)

? ? ? ? ? ? self.tabBar.addSubview(tabButton)


? ? ? ? }

? ? }

//點擊事件

? ? varbeforeTag:Int=-1

? ? @objcfunctabbarSelect(sender:UIButton) {


? ? ? ? ifbeforeTag!=-1{

? ? ? ? ? ? lettabButton:UIButton=self.view.viewWithTag(beforeTag)as!UIButton

? ? ? ? ? ? tabButton.setImage([ imageLiteral(resourceName: "Jurisdiction_customer_normal"), imageLiteral(resourceName: "Visit_rankinge_normal"), imageLiteral(resourceName: "Sales_architecture_normal")][beforeTag-110], for:UIControlState.normal)

? ? ? ? ? ? tabButton.setTitleColor(UIColor.init(red:1/255.0, green:142/255.0, blue:200/255.0, alpha:1), for:UIControlState.normal)

? ? ? ? ? ? sender.setImage([? imageLiteral(resourceName: "Jurisdiction_customer_pressed"), imageLiteral(resourceName: "Visit_rankinge_pressed"), imageLiteral(resourceName: "Sales_architecture_pressed")][sender.tag-110], for:UIControlState.normal)

? ? ? ? ? ? sender.setTitleColor(UIColor.white, for: UIControlState.normal)

? ? ? ? }else{

? ? ? ? ? ? ifsender.tag!=110{

? ? ? ? ? ? ? ? sender.setImage([? imageLiteral(resourceName: "Jurisdiction_customer_pressed"), imageLiteral(resourceName: "Sales_architecture_pressed"), imageLiteral(resourceName: "Visit_rankinge_pressed")][sender.tag-110], for:UIControlState.normal)

? ? ? ? ? ? ? ? sender.setTitleColor(UIColor.white, for:UIControlState.normal)

? ? ? ? ? ? ? ? lettabButton:UIButton=self.view.viewWithTag(110)as!UIButton

? ? ? ? ? ? ? ? tabButton.setImage( imageLiteral(resourceName: "Jurisdiction_customer_normal"), for:UIControlState.normal)

? ? ? ? ? ? ? ? tabButton.setTitleColor(UIColor.init(red:1/255.0, green:142/255.0, blue:200/255.0, alpha:1), for:UIControlState.normal)

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? beforeTag= sender.tag



? ? ? ? tabSelectIma.tag= sender.tag-10

? ? ? ? UIView.animate(withDuration:0.2) {

? ? ? ? ? ? self.tabSelectIma.frame=CGRect.init(x:screenWidth/3*CGFloat.init(sender.tag-110), y:0, width:self.tabSelectIma.bounds.size.width, height:49)

? ? ? ? }

? ? }

//橫屏代理

? ? overridefuncviewWillTransition(to size:CGSize, with coordinator:UIViewControllerTransitionCoordinator) {

? ? ? ? screenWidth= size.width

? ? ? ? tabSelectIma.image=UtilitiesTools.shared().tabSelectionImage(size: size)


? ? ? ? tabSelectIma.frame=CGRect.init(x: size.width/3*CGFloat.init(tabSelectIma.tag-100), y:0, width: size.width/3, height:49)


? ? ? ? tabLine1.frame=CGRect.init(x: size.width/3, y:0, width:? imageLiteral(resourceName: "img_tab_parting").size.width, height:? imageLiteral(resourceName: "img_tab_parting").size.height)

? ? ? ? tabLine2.frame=CGRect.init(x: size.width/3*2, y:0, width:? imageLiteral(resourceName: "img_tab_parting").size.width, height:? imageLiteral(resourceName: "img_tab_parting").size.height)

//? ? ? ? self.tabBar.frame = CGRect.init(x: 0, y: 49, width: size.width, height: 49.0)

//? ? ? ? print(self.tabBar.frame)



? ? ? ? ifsize.width> size.height{

? ? ? ? ? ? 3.timesNum{ (time)in

? ? ? ? ? ? ? ? letbarButton1 =self.view.viewWithTag(110+time-1)


? ? ? ? ? ? ? ? if#available(iOS11.0, *) {

? ? ? ? ? ? ? ? ? ? barButton1?.frame=CGRect.init(x:screenWidth/3*CGFloat.init(time-1), y:-7, width:screenWidth/3, height:49)

? ? ? ? ? ? ? ? }else{

? ? ? ? ? ? ? ? ? ? barButton1?.frame=CGRect.init(x:screenWidth/3*CGFloat.init(time-1), y:0, width:screenWidth/3, height:49)

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }else{

? ? ? ? ? ? 3.timesNum{ (time)in

? ? ? ? ? ? ? ? letbarButton1 =self.view.viewWithTag(110+time-1)

? ? ? ? ? ? ? ? barButton1?.frame=CGRect.init(x:screenWidth/3*CGFloat.init(time-1), y:0, width:screenWidth/3, height:49)

? ? ? ? ? ? }

? ? ? ? }

? ? }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末目尖,一起剝皮案震驚了整個濱河市馒吴,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖饮戳,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件豪治,死亡現(xiàn)場離奇詭異,居然都是意外死亡扯罐,警方通過查閱死者的電腦和手機负拟,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來歹河,“玉大人掩浙,你說我怎么就攤上這事〗掌纾” “怎么了厨姚?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長键菱。 經(jīng)常有香客問我谬墙,道長,這世上最難降的妖魔是什么经备? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任拭抬,我火速辦了婚禮,結(jié)果婚禮上侵蒙,老公的妹妹穿的比我還像新娘造虎。我一直安慰自己,他們只是感情好纷闺,可當(dāng)我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布累奈。 她就那樣靜靜地躺著,像睡著了一般急但。 火紅的嫁衣襯著肌膚如雪澎媒。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天波桩,我揣著相機與錄音戒努,去河邊找鬼。 笑死镐躲,一個胖子當(dāng)著我的面吹牛储玫,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播萤皂,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼撒穷,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了裆熙?” 一聲冷哼從身側(cè)響起端礼,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤禽笑,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后蛤奥,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體佳镜,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年凡桥,在試婚紗的時候發(fā)現(xiàn)自己被綠了蟀伸。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡缅刽,死狀恐怖啊掏,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情衰猛,我是刑警寧澤迟蜜,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站腕侄,受9級特大地震影響小泉,放射性物質(zhì)發(fā)生泄漏芦疏。R本人自食惡果不足惜冕杠,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望酸茴。 院中可真熱鬧分预,春花似錦、人聲如沸薪捍。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽酪穿。三九已至凳干,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間被济,已是汗流浹背救赐。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留只磷,地道東北人经磅。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像钮追,于是被迫代替她去往敵國和親预厌。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,044評論 2 355

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