选中的背景是图片类型,我的项目是一个颜色渐变的图片,正常使用时切三套图可以,可是横屏后,图片就不够大了,还有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(["辖区客户","拜访排名","销售架构"][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)
}
}
}
网友评论