Swift爬行篇-- UIButton

Swift爬行篇-- UIButton,第1张

概述1. 生成UIButton btn:UIButton = UIButton(type: UIButtonType.Custom )as UIButton //初始化button的对象和风格 btn.frame = (CGRect(origin: CGPointMake(10.0, 110.0), size: CGSizeMake(150,50))) //设定but

1. 生成UIbutton

        btn:UIbutton = UIbutton(type: UIbuttonType.Custom )as UIbutton  //初始化button的对象和风格        btn.frame = (CGRect(origin: CGPointMake(10.0,110.0),size: CGSizeMake(150,50))) //设定button的位置        btn.setTitle("hello",forState:UIControlState.normal)              //设置button的显示文字        btn.backgroundcolor = UIcolor.redcolor()                           //设置背景颜色         //设置该button的响应函数,函数名称后面加上: 表示传递触摸对象, 没有则不传递</span>                 btn.addTarget(self,action: "buttonClick:",forControlEvents: UIControlEvents.touchUpInsIDe)        btn.setTitlecolor(UIcolor.blackcolor(),forState: UIControlState.normal)     //设置文字颜色        self.vIEw.addSubvIEw(btn)   //设置button界面显示         //设定的空间响应函数                 func buttonClick(sender: UIbutton!){                  }



2.UIbutton风格

来源:https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIbutton_Class/#//apple_ref/c/tdef/UIbuttonType

DeclarationSWIFTenum UIbuttonType : Int {    case Custom    case System    case Detaildisclosure    case Infolight    case InfoDark    case ContactAdd    static var RoundedRect: UIbuttonType { get }}ConstantsCustomNo button style.Available in iOS 2.0 and later.SystemA system style button,such as those shown in navigation bars and toolbars.Available in iOS 7.0 and later.DetaildisclosureA detail disclosure button.Available in iOS 2.0 and later.InfolightAn information button that has a light background.Available in iOS 2.0 and later.InfoDarkAn information button that has a dark background.Available in iOS 2.0 and later.ContactAddA contact add button.Available in iOS 2.0 and later.RoundedRectA rounded-rectangle style button.Use UIbuttonTypeSystem instead.PS:RoundedRect 该风格在Xcode5中已经被取消了,默认生成出来的button都是直角的,可通过代码手动设置圆角的风格(在属性vIEw也没有该属性)<btn.layer.cornerRadius = 5/设置四角圆弧的曲度<btn.layer.borderWIDth = 1//设置边框的宽度</span>btn.layer.bordercolor = UIcolor.whitecolor.CGcolor //设置边框的颜色   没特殊情况下,边框的宽度和颜色其实都是不需要设置的</span>



3. button的使能

btn.enabled = false //设置按钮不能点击,true为使能


4.部分圆角button和折角button
//部分圆角按钮,主要是利用layer的mask属性,在通过CAShaperLayer和UIBezIErPath来画        var btn7:UIbutton = UIbutton(frame: CGRect(x: 50,y: 330,wIDth: 100,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)


5.创建有图片的button
 //创建一个图片加文字的按钮        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)        


6.创建图片背景的button

        btn.setBackgroundImage(UIImage(named:"background"),forState:.normal)




资料来源:https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIbutton_Class/#//apple_ref/c/tdef/UIbuttonType

http://www.wutongwei.com/front/infor_showone.tweb?ID=88

总结

以上是内存溢出为你收集整理的Swift爬行篇-- UIButton全部内容,希望文章能够帮你解决Swift爬行篇-- UIButton所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1085501.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存