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)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容