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
- 3.UIButton
- UIButton
- UIButton
- UIButton
- uibutton
- UIButton
- UIButton
- UIButton
- UIButton
- UIButton
- UIButton
- UIBUTTON
- UIButton
- UIButton
- UIButton
- UIBUTTON
- UIButton
- UIButton
- 源码安装postgres,新建数据库用户遇到的问题
- 如何悄悄的访问谷歌
- iOS开发:Apply Pay商家接入指南-基础介绍
- 利用nginx来屏蔽指定的user_agent的访问
- getLocationInWindow 和 getLocationOnScreen的区别 以及computeVerticalScrollOffset
- 3.UIButton
- Using the SafetyNet API
- adb在restart时候失败的解决方法以及其他的几个问题
- behemoth - 04
- android 中9patch图片的制作
- rabbitmq(三) 消息确认
- java中的工厂模式(简单工厂模式+工厂方法模式)
- java之redis篇(spring-data-redis整合)
- 颜色的搭配