在设置rootVIEwController之后,在这个control里的vIEwDIDLoad方法里,添加相应的代码.设置了9个不同类型的UIbutton
创建了有状态的button,以及添加button事件@H_403_5@
创建没有状态的button
创建一个图片加文字的按钮,只要图片就不需要设置Title
从系统定义的button类型创建button
创建禁止button
创建一个圆角button
部分圆角button,主要是利用layer的mask属性,在通过CAShaperLayer和UIBezIErPath来画
创建折角button
创建border的button
import UIKitclass VIEwController: UIVIEwController { overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() //创建有状态的按钮 var btn:UIbutton = UIbutton(frame: CGRect(x: 50,y: 50,wIDth: 100,height: 25)) btn.setTitle("点击按钮",forState: UIControlState.Highlighted) btn.setTitle("未点击",forState: UIControlState.normal) btn.backgroundcolor = UIcolor.blackcolor() self.vIEw.addSubvIEw(btn) //给按钮添加点击事件 btn.addTarget(self,action: "clickEvent:",forControlEvents: UIControlEvents.touchUpInsIDe) //创建没有状态的按钮 var btn2:UIbutton = UIbutton(frame: CGRect(x: 50,y: 85,height: 35)) btn2.setTitle("normal",forState: UIControlState.normal) btn2.backgroundcolor = UIcolor.bluecolor() self.vIEw.addSubvIEw(btn2) //创建一个图片加文字的按钮 var btn3:UIbutton = UIbutton(frame: CGRect(x: 50,y: 130,wIDth: 180,height: 35)) btn3.setimage(UIImage(named: "btn1"),forState: UIControlState.normal) btn3.TitleLabel?.Font = UIFont.boldSystemFontOfSize(30) btn3.imageVIEw?.contentMode = UIVIEwContentMode.ScaleAspectFit //btn3.imageEdgeInsets = UIEdgeInsets(top: 0,left: 0,bottom: 0,right: 0) btn3.setTitle("图片按钮",forState: UIControlState.normal) self.vIEw.addSubvIEw(btn3) //从系统定义的按钮类型创建button var btn4:UIbutton = UIbutton.buttonWithType(UIbuttonType.ContactAdd) as! UIbutton //btn4.backgroundcolor = UIcolor.browncolor() btn4.frame.offset(dx: 60,dy: 200) self.vIEw.addSubvIEw(btn4) //创建禁止按钮 var btn5:UIbutton = UIbutton(frame: CGRect(x: 50,y: 240,height: 35)) btn5.setTitle("点击按钮",forState: UIControlState.Highlighted) btn5.setTitle("禁止按钮",forState: UIControlState.normal) btn5.enabled = false //设置按钮不能点击 btn5.setTitlecolor(UIcolor.redcolor(),forState: UIControlState.Disabled)//代表已经禁止 btn5.backgroundcolor = UIcolor.purplecolor() self.vIEw.addSubvIEw(btn5) //创建一个圆角按钮 var btn6:UIbutton = UIbutton(frame: CGRect(x: 50,y: 280,height: 35)) btn6.backgroundcolor = UIcolor.whitecolor() btn6.setTitlecolor(UIcolor.blackcolor(),forState: UIControlState.normal) btn6.setTitle("圆角按钮",forState: UIControlState.normal) btn6.layer.cornerRadius = 5 self.vIEw.addSubvIEw(btn6) //部分圆角按钮,在通过CAShaperLayer和UIBezIErPath来画 var btn7:UIbutton = UIbutton(frame: CGRect(x: 50,y: 330,height: 35)) btn7.backgroundcolor = UIcolor.whitecolor() btn7.setTitlecolor(UIcolor.blackcolor(),forState: UIControlState.normal) btn7.setTitle("部分圆角按钮",forState: UIControlState.normal) let shape:CAShapeLayer = CAShapeLayer() let bepath:UIBezIErPath = UIBezIErPath(roundedRect: btn6.bounds,byRoundingCorners: UIRectCorner.topRight|UIRectCorner.topleft,cornerRadii: CGSize(wIDth: 15,height: 15)) UIcolor.blackcolor().setstroke() shape.path = bepath.CGPath btn7.layer.mask = shape self.vIEw.addSubvIEw(btn7) //创建折角按钮 var btn8:UIbutton = UIbutton(frame: CGRect(x: 50,y: 380,height: 35)) btn8.backgroundcolor = UIcolor.whitecolor() btn8.setTitlecolor(UIcolor.blackcolor(),forState: UIControlState.normal) btn8.setTitle("折角按钮",forState: UIControlState.normal) let shape8:CAShapeLayer = CAShapeLayer() let bepath8:UIBezIErPath = UIBezIErPath() bepath8.movetoPoint(CGPoint(x: 0,y: 0)) bepath8.addlinetoPoint(CGPoint(x: 80,y: 0)) bepath8.addlinetoPoint(CGPoint(x: 100,y: 15)) bepath8.addlinetoPoint(CGPoint(x: 100,y: 35)) bepath8.addlinetoPoint(CGPoint(x: 0,y: 35)) bepath8.closePath() shape8.path = bepath8.CGPath btn8.layer.mask = shape8 self.vIEw.addSubvIEw(btn8) //创建border按钮 var btn9:UIbutton = UIbutton(frame: CGRect(x: 50,y: 420,height: 35)) btn9.backgroundcolor = UIcolor.whitecolor() btn9.setTitle("边框按钮",forState: UIControlState.normal) btn9.setTitlecolor(UIcolor.blackcolor(),forState: UIControlState.normal) btn9.layer.bordercolor = UIcolor.blackcolor().CGcolor btn9.layer.borderWIDth = 1 btn9.layer.cornerRadius = 5 self.vIEw.addSubvIEw(btn9) // } overrIDe func dIDReceiveMemoryWarning() { super.dIDReceiveMemoryWarning() // dispose of any resources that can be recreated. } func clickEvent(sender:AnyObject){ NSLog("按钮点击了事件") } }
效果如下
转自:http://www.wutongwei.com/front/infor_showone.tweb?ID=88
总结以上是内存溢出为你收集整理的THE SWIFT CODE之设置UIBUTTON的不同方式创建,以及不同的状态和外观全部内容,希望文章能够帮你解决THE SWIFT CODE之设置UIBUTTON的不同方式创建,以及不同的状态和外观所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)