Swift_UIButton

UIButton的创建方式:

1.
let button = UIButton(frame: CGRectMake(10, 30, 60, 50))
2.
var button:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
        
        button.frame = CGRectMake(10, 100, 100, 50)

基本使用

   func setButton() {
        let button = UIButton(frame: CGRectMake(10, 30, 100, 40))
        //设置背景颜色
        button.backgroundColor = UIColor.brownColor()
        //设置标题
        button.setTitle("点击", forState: UIControlState.Normal)
        //设置标题颜色
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        //设置图片
        button.setImage(UIImage(named: "buttonadd"), forState:UIControlState.Normal)
        //设置图片偏移量
        button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 20)
        //添加触发事件 如果事件需要传递参数 即在方法名后添加':' eg:"swiftBtnClick:"
        button.addTarget(self, action:Selector("swiftBtnClick:"), forControlEvents:UIControlEvents.TouchUpInside)
        //添加到视图上
        self.view.addSubview(button)
    }
    
    func swiftBtnClick(button:UIButton) {
        println("被点击")
    }

这里需要注意的是 如果按钮的触发方法带参数 需要在Selector里面填写方法名称后面添加冒号
<span style="color:#ffcccc;"> </span> func setHeighLightButton() {
        var hlBtn = UIButton.buttonWithType(UIButtonType.System) as! UIButton
        
        hlBtn.frame = CGRectMake(10, 160, 100, 40)
        hlBtn.setTitle("普通", forState: UIControlState.Normal)
//        hlBtn.setTitle("高亮", forState: UIControlState.Highlighted)
        hlBtn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        hlBtn.setTitle("以选中", forState: UIControlState.Selected)
        hlBtn.setTitleColor(UIColor.redColor(), forState: UIControlState.Selected)
        
        hlBtn.setBackgroundImage(UIImage(named: "backbutton_green"), forState: UIControlState.Normal)
        hlBtn.setBackgroundImage(UIImage(named: "backbutton_yellow"), forState: UIControlState.Highlighted)
        hlBtn.addTarget(self, action: Selector("hlBtnClick"), forControlEvents: UIControlEvents.TouchUpInside)
        
        //引用
        _lightBtn = hlBtn
        
        self.view.addSubview(hlBtn)
    }
    
    func hlBtnClick() {
        _lightBtn!.selected = !_lightBtn!.selected
        
        //.currentTitle 只能打印Normal状态下的按钮标题
        println(_lightBtn!.titleForState(UIControlState.Selected))
        
    }

常用API

    func setTitle(title: String?, forState state: UIControlState) // 设置那种状态下的标题
    func setTitleColor(color: UIColor?, forState state: UIControlState) // 设置哪种状态下的标题颜色
    func setTitleShadowColor(color: UIColor?, forState state: UIControlState) //设置哪种状态下的标题阴影颜色
    func setImage(image: UIImage?, forState state: UIControlState) // 设置图片 需要保证不同状态下图片尺寸相同
    func setBackgroundImage(image: UIImage?, forState state: UIControlState) // 设置背景图片
    @availability(iOS, introduced=6.0)
    func setAttributedTitle(title: NSAttributedString!, forState state: UIControlState) //利用属性字符串 保证标题为单行
    
    
    //获取当前状态下的按钮一些属性
    func titleForState(state: UIControlState) -> String? // these getters only take a single state value
    func titleColorForState(state: UIControlState) -> UIColor?
    func titleShadowColorForState(state: UIControlState) -> UIColor?
    func imageForState(state: UIControlState) -> UIImage?
    func backgroundImageForState(state: UIControlState) -> UIImage?
    @availability(iOS, introduced=6.0)
    func attributedTitleForState(state: UIControlState) -> NSAttributedString?


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值