UIbutton是我们经常用的一个控件,它是继承自UIControl的,下面就总结一下相关属性及用法:
1.UIbutton的创建
a.不带样式的:
let btn: UIbutton = UIbutton()
b.带样式的:
let btns:UIbutton =UIbutton(type: UIbuttonType);
其中UIbuttonType是一个枚举如下所示:
publicenum UIbuttonType :Int { case Custom // no button type 自定义样式按钮 @available(iOS 7.0,*) case System // standard system button //系统的样式按钮 case Detaildisclosure case Infolight case InfoDark case ContactAdd//联系人按钮 public staticvar RoundedRect: UIbuttonType { get }// Deprecated,use UIbuttonTypeSystem instead}
用法如下:
let btn: UIbutton = UIbutton(type: .Custom)
2.UIbutton设置字内容和颜色
btn.setTitle("按钮",forState: .normal)
btn.setTitlecolor(UIcolor.whitecolor(),forState: .normal)
3.UIbutton设置背景颜色和背景图片
btn.backgroundcolor =UIcolor.blackcolor()
btn.setBackgroundImage(UIImage(named:"1"),forState: .normal)
4.UIbutton设置字体大小
btn.TitleLabel?.Font =UIFont.systemFontOfSize(20)
5.禁用UIbutton
btn.enabled =false //禁止按钮,默认为true
6.设置圆角
btn.layer.cornerRadius = 8
7.设置背景图片为圆角(因为我们直接设置UIbutton圆角时,图片不会变为圆角)
buttonImage.setimage(UIImage(named:"1"),forState: UIControlState.normal)
//设置背景图片为圆角
buttonImage.imageVIEw?.layer.cornerRadius = 50
8.在UIbutton上添加图片和文字,有时需要我们调整,此时需要:
方向为逆时针方向,上、左、下、右依次去设置的
btn.imageEdgeInsets =UIEdgeInsetsMake(top: CGfloat,left: CGfloat,bottom: CGfloat,right: CGfloat)
btn.TitleEdgeInsets =UIEdgeInsetsMake(top: CGfloat,right: CGfloat)
例子如下:
//创建一个图片一个文字的按钮 let btn2: UIbutton = UIbutton(type: .Custom) btn2.frame = CGRectMake(50,100,120,35) btn2.setimage(UIImage(named: "1"),forState: .normal) btn2.backgroundcolor = UIcolor.blackcolor() btn2.TitleLabel?.Font = UIFont.systemFontOfSize(20) btn2.imageVIEw?.contentMode = UIVIEwContentMode.ScaleAspectFit btn2.setTitle("图片按钮",forState: .normal) //偏移量,分别为上下左右 btn2.imageEdgeInsets = UIEdgeInsetsMake(0,-50,0) btn2.TitleEdgeInsets = UIEdgeInsetsMake(0,-80,5) btn2.setTitlecolor(UIcolor.whitecolor(),forState: .normal) btn2.adjustsImageWhenHighlighted = false self.vIEw.addSubvIEw(btn2)9 .添加按钮的点击事件
第一种是不带参数的,第二种是带参数的
btn.addTarget(self,action:"click",forControlEvents: .touchUpInsIDe)
btn.addTarget(self,action:"clicks:",forControlEvents: .touchUpInsIDe)
第一种:
func click(){}
第二种:
func clicks(sender:UIbutton){}
写的代码如下:
func initbuttonVIEw() { //创建有状态的按钮 let btn: UIbutton = UIbutton(type: .Custom) btn.frame = CGRectMake(50,20,30) btn.setTitle("点击按钮",forState: .Selected) btn.setTitle("未点击",forState: .normal) btn.backgroundcolor = UIcolor.blackcolor() btn.addTarget(self,action: "clickBtn:",forControlEvents: .touchUpInsIDe) self.vIEw .addSubvIEw(btn) //创建无状态的按钮 let btn1: UIbutton = UIbutton() btn1.frame = CGRectMake(50,60,30) btn1.setTitle("normal",forState: .normal) btn1.backgroundcolor = UIcolor.bluecolor() self.vIEw.addSubvIEw(btn1) //创建一个图片一个文字的按钮 let btn2: UIbutton = UIbutton(type: .Custom) btn2.frame = CGRectMake(50,forState: .normal) btn2.adjustsImageWhenHighlighted = false self.vIEw.addSubvIEw(btn2) //创建禁止按钮 let btn3: UIbutton = UIbutton(type: .Custom) btn3.frame = CGRectMake(50,140,35) btn3.setTitle("点击按钮",forState: .Highlighted) btn3.setTitle("禁止按钮",forState: .normal) btn3.enabled = false //禁止按钮,默认为true btn3.setTitlecolor(UIcolor.redcolor(),forState: .Disabled) btn3.backgroundcolor = UIcolor.purplecolor() self.vIEw.addSubvIEw(btn3) //创建圆角按钮 let btn4: UIbutton = UIbutton(type: .Custom) btn4.frame = CGRectMake(50,180,35) btn4.backgroundcolor = UIcolor.blackcolor() btn4.setTitle("圆角按钮 ",forState: .normal) btn4.setTitlecolor(UIcolor.whitecolor(),forState: .normal) btn4.layer.cornerRadius = 8 self.vIEw.addSubvIEw(btn4) //部分圆角按钮,主要利用layer的mask属性,在tongguoCAShaperLayer和UIBezIErPath来画 let btn5: UIbutton = UIbutton(type: .Custom) btn5.frame = CGRectMake(50,220,35) btn5.backgroundcolor = UIcolor.blackcolor() btn5.setTitle("部分圆角按钮",forState: .normal) btn5.setTitlecolor(UIcolor.whitecolor(),forState: .normal) let shape: CAShapeLayer = CAShapeLayer() let bepath: UIBezIErPath = UIBezIErPath(roundedRect: btn5.bounds,byRoundingCorners: [UIRectCorner.topRight,UIRectCorner.topleft,UIRectCorner.Bottomleft],cornerRadii: CGSize(wIDth: 8,height: 8)) UIcolor.blackcolor().setstroke() shape.path = bepath.CGPath btn5.layer.mask = shape self.vIEw.addSubvIEw(btn5) //创建带边框的按钮 let btn6: UIbutton = UIbutton(type: .Custom) btn6.frame = CGRectMake(50,260,35) btn6.setTitle("边框按钮",forState: .normal) btn6.setTitlecolor(UIcolor.blackcolor(),forState: .normal) btn6.layer.bordercolor = UIcolor.blackcolor().CGcolor btn6.layer.borderWIDth = 1 btn6.layer.cornerRadius = 8 self.vIEw.addSubvIEw(btn6) //显示提示信息的UILabel labelText = UILabel() labelText.frame = CGRectMake(50,300,44) labelText.textcolor = UIcolor.orangecolor() self.vIEw.addSubvIEw(labelText) let btns: UIbutton = UIbutton(type: .Custom) btns.frame = CGRectMake(<#T##x: CGfloat##CGfloat#>,<#T##y: CGfloat##CGfloat#>,<#T##wIDth: CGfloat##CGfloat#>,<#T##height: CGfloat##CGfloat#>) btn.setTitle("按钮",forState: .normal) btn.setTitlecolor(UIcolor.whitecolor(),forState: .normal) btn.backgroundcolor = UIcolor.blackcolor() btn.setBackgroundImage(UIImage(named: "1"),forState: .normal) btn.imageEdgeInsets = UIEdgeInsetsMake(<#T##top: CGfloat##CGfloat#>,<#T##left: CGfloat##CGfloat#>,<#T##bottom: CGfloat##CGfloat#>,<#T##right: CGfloat##CGfloat#>) btn.TitleEdgeInsets = UIEdgeInsetsMake(<#T##top: CGfloat##CGfloat#>,<#T##right: CGfloat##CGfloat#>) btn.addTarget(self,action: "click",forControlEvents: .touchUpInsIDe) } func clickBtn(sender: UIbutton){ sender.selected = !sender.selected labelText.text = "点击了按钮" }
效果图如下:
总结
以上是内存溢出为你收集整理的swift中UIButton的简单使用全部内容,希望文章能够帮你解决swift中UIButton的简单使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)