Swift爬行篇-- UIButton

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, width: 100, 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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值