選中的背景是圖片類型肤寝,我的項目是一個顏色漸變的圖片育苟,正常使用時切三套圖可以,可是橫屏后醋闭,圖片就不夠大了礼搁,還有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)
? ? ? ? ? ? }
? ? ? ? }
? ? }