加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift爬行篇-- UIButton

发布时间:2020-12-14 01:55:20 所属栏目:百科 来源:网络整理
导读: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:UICon

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

Declaration
SWIFT
enum UIButtonType : Int {    
case Custom    
case System    c
ase DetailDisclosure    
case InfoLight    
case InfoDark    
case ContactAdd    
static var RoundedRect: UIButtonType { get }}
Constants
Custom
No button style.
Available in iOS 2.0 and later.
System
A system style button,such as those shown in navigation bars and toolbars.
Available in iOS 7.0 and later.
DetailDisclosure
A detail disclosure button.
Available in iOS 2.0 and later.
InfoLight
An information button that has a light background.
Available in iOS 2.0 and later.
InfoDark
An information button that has a dark background.
Available in iOS 2.0 and later.
ContactAdd
A contact add button.
Available in iOS 2.0 and later.
RoundedRect
A 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读