swift之按钮的使用

  let phoneBtn:UIButton=UIButton.init(frame:CGRect(x:0,y:0,width:WIDTH,height:50))

        phoneBtn.setTitle("客服热线:4000-010-313", for:UIControlState.normal)

        phoneBtn.setTitleColor(UIColor.init(red:59.0/255, green:122.0/255, blue:246.0/255, alpha:1), for: UIControlState.normal)

        phoneBtn.setImage(UIImage.init(named:"bottomPhone"), for:UIControlState.normal)

        phoneBtn.imageEdgeInsets=UIEdgeInsets(top:0, left: -20, bottom:0, right: 0)

          phoneBtn.titleEdgeInsets=UIEdgeInsets(top:0, left: 0, bottom:0, right: 0)

        tab.tableFooterView=phoneBtn

        phoneBtn.addTarget(self, action:#selector(phoneCall), for:UIControlEvents.touchUpInside)

       

        //上啦刷新

       

        

    }

    @objc func phoneCall() {

    print("打电话")

    }



==============按钮的封装,拿来即用======

 //开始按钮

        let rect:CGRect=CGRect.init(x: 10, y: 200, width: 50, height: 50)

        let font:UIFont=UIFont.systemFont(ofSize: 15)

        let color:UIColor=UIColor.red

        let bgcolor:UIColor=UIColor.white

        let acs:Selector=#selector(ac)//事件

        let btn:UIButton=setButton(rect: rect, title: "开始", htitle: nil, font: font, hfont: nil, titleColor: color, htitleColor: nil, imageName: nil, himageName: nil,bgcolor: bgcolor,action: acs,target:self)

        view.addSubview(btn)



 //开始动画

    @objc func ac(){

         imagv.startAnimating()//开始动画

    }

    

    

   //创建按钮

func setButton(rect:CGRect,title:String?,htitle:String?,font:UIFont?,hfont:UIFont?,titleColor:UIColor?,htitleColor:UIColor?,imageName:String?,himageName:String?,bgcolor:UIColor?,action:Selector?,target:UIViewController?)->UIButton{

        //参数依次是定位,标题,选中时的标题,标题字体大小,选中时的标题字体大小,标题颜色,选中时的标题颜色,图片,选中时的图片

        let btn:UIButton=UIButton.init(frame: rect)

        if htitle != nil && htitle != ""{//选中时的标题

        btn.setTitle(htitle, for: UIControlState.highlighted)

        }else if title != nil && title != ""{//

            btn.setTitle(title, for: UIControlState.normal)

        }

        if hfont != nil{//选中时标题字体大小

             btn.titleLabel?.font=hfont

        }else if font != nil{//

        btn.titleLabel?.font=font

        }

        if htitleColor != nil{//选中时标题颜色

             btn.setTitleColor(htitleColor, for: UIControlState.highlighted)

        }else if titleColor != nil{//

        btn.setTitleColor(titleColor, for: UIControlState.normal)

        }

        

        if himageName != nil && himageName != ""{//选中时的图片

            btn.setImage(UIImage.init(named: himageName!), for: UIControlState.highlighted)

        }else if imageName != nil && imageName != ""{//

             btn.setImage(UIImage.init(named: imageName!), for: UIControlState.normal)

        }

        if bgcolor != nil{//背景颜色

            btn.backgroundColor=bgcolor

        }

        if action != nil{//添加事件

            btn.addTarget(target, action: action!, for: UIControlEvents.touchUpInside)

        }

        return btn

    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值