3.UIButton

来源:互联网 发布:淘宝助理 二次验证 编辑:程序博客网 时间:2024/05/29 13:35

UIButton

UIButton即按钮控件, 下面我们学习使用它

1. 创建并设置不同状态按钮的文字和颜色

import UIKitimport AlamofireImageclass ViewController: UIViewController {    override func viewDidLoad() {        super.viewDidLoad()        let button = UIButton(type: .Custom)        button.frame = CGRect(x: 0, y: 10, width: 100, height: 60)        // 设置不同状态文字        button.setTitle("Normal", forState: .Normal)        button.setTitle("Highlighted", forState: .Highlighted)        button.setTitle("Disabled", forState: .Disabled)        // 设置不同状态文字颜色        button.setTitleColor(UIColor.redColor(), forState: .Normal)        button.setTitleColor(UIColor.blueColor(), forState: .Highlighted)        button.setTitleColor(UIColor.grayColor(), forState: .Disabled)        self.view.addSubview(button)    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()    }}

正常状态:
正常状态

触摸状态:
触摸状态

禁用状态:

button.enabled = false

禁用状态

2. 设置不同状态的图片

button.setImage(UIImage(named: "normal"), forState: .Normal)button.setImage(UIImage(named: "highlighted"), forState: .Highlighted)button.setImage(UIImage(named: "disabled"), forState: .Disabled)

正常状态:
正常状态

触摸状态:
触摸状态

3. 设置不同状态的背景图片

button.setBackgroundImage(UIImage(named: "normal"), forState: .Normal)button.setBackgroundImage(UIImage(named: "highlighted"), forState: .Highlighted)button.setBackgroundImage(UIImage(named: "disabled"), forState: .Disabled)

运行程序, 我们可以看见,
正常状态:
正常状态

触摸状态:
触摸状态

4. UIButton的触摸事件

当我们触摸一个按钮的时候, 会执行一些逻辑.

button.addTarget(self, action: "buttonAction:", forControlEvents: .TouchUpInside)func buttonAction(sender: UIButton) {    print(sender.titleForState(sender.state))}

当我们点击按钮的时候, 控制台会打印出Optional(“Highlighted”)

5. 完整代码

import UIKitclass ViewController: UIViewController {    override func viewDidLoad() {        super.viewDidLoad()        let button = UIButton(type: .Custom)        button.frame = CGRect(x: 0, y: 10, width: 100, height: 40)        // 设置不同状态文字        button.setTitle("Normal", forState: .Normal)        button.setTitle("Highlighted", forState: .Highlighted)        button.setTitle("Disabled", forState: .Disabled)        // 设置不同状态文字颜色        button.setTitleColor(UIColor.redColor(), forState: .Normal)        button.setTitleColor(UIColor.blueColor(), forState: .Highlighted)        button.setTitleColor(UIColor.grayColor(), forState: .Disabled)        // 设置不同状态的图片//        button.setImage(UIImage(named: "normal"), forState: .Normal)//        button.setImage(UIImage(named: "highlighted"), forState: .Highlighted)//        button.setImage(UIImage(named: "disabled"), forState: .Disabled)        button.setBackgroundImage(UIImage(named: "normal"), forState: .Normal)        button.setBackgroundImage(UIImage(named: "highlighted"), forState: .Highlighted)        button.setBackgroundImage(UIImage(named: "disabled"), forState: .Disabled)        button.addTarget(self, action: "buttonAction:", forControlEvents: .TouchUpInside)//        button.enabled = false        self.view.addSubview(button)    }    func buttonAction(sender: UIButton) {        print(sender.titleForState(sender.state))    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()    }}
3 0
原创粉丝点击