UIButton
来源:互联网 发布:北海旅游 知乎 编辑:程序博客网 时间:2024/04/20 23:55
简单介绍一下swift中UIButton的创建
let button = UIButton.init(type: UIButtonType.custom)
button.frame = CGRect.init(x: 100, y: 150, width: 80, height: 40)
button.backgroundColor = UIColor.red
button.tag = 1; //自己根据需要设置
self.view.addSubview(button)
创建了一个简单的UIButton
现在讲解一下所包含的属性等内容
1、按钮的类型有一下几种常用到类型
UIButtonType.custom //定制按钮,不带图标,默认文字为白色,无接触时的高亮效果
UIButtonType.contactAdd //“+”图标按钮,默认文字颜色为蓝色,有接触时的高亮效果
UIButtonType.detailDisclosure //"!"图标按钮,默认文字为蓝色,有接触时的高亮效果
UIButtonType.system //不带图标,默认文字颜色为蓝色,有接触时的高亮效果
2、按钮的文字、颜色和状态
button.setTitle("普通状态", for: UIControlState.normal)
button.setTitle("接触状态", for: UIControlState.highlighted)
button.setTitle("禁用状态", for: UIControlState.disabled)
//文字颜色
button.setTitleColor(UIColor.blue, for: UIControlState.normal)
button.setTitleColor(UIColor.black, for:.highlighted) //swift能自动识别,有时候这样写就可以
button.setTitleColor(UIColor.white, for: .disabled)
//阴影文字颜色
button.setTitleShadowColor(UIColor.orange, for: UIControlState.normal)
button.setTitleShadowColor(UIColor.orange, for: UIControlState.highlighted)
button.setTitleShadowColor(UIColor.orange, for: UIControlState.disabled)
3、按钮图片
button.setImage(UIImage(named:"图片名称"), for: .normal)
button.setBackgroundImage(UIImage(named:"图片名称"), for:.normal)
4、按钮点击去除点击阴影效果
button.adjustsImageWhenHighlighted = false
5、按钮的点击事件
//当不需要传递参数时候
button.addTarget(self, action: #selector(click), for: UIControlEvents.touchUpInside)
func click() {
}
//当传递参数时候
button.addTarget(self, action: #selector(clickBtn(_:)), for: UIControlEvents.touchUpInside)
func clickBtn(_ cl: UIButton) {
cl.setTitle("点击了", for: UIControlState.normal)
}
6、按钮的弧度设置
button.layer.masksToBounds = true
button.layer.cornerRadius = 10
以上为简单的使用,如果需要更复杂的操作需要自己去处理
- UIButton
- UIButton
- UIButton
- uibutton
- UIButton
- UIButton
- UIButton
- UIButton
- UIButton
- UIButton
- UIBUTTON
- UIButton
- UIButton
- UIButton
- UIBUTTON
- UIButton
- UIButton
- UIButton
- Spring Boot引用本地jar包
- 最小二乘法 拟合平面直线
- No6.等待线程结束(join)和通知(notify)
- WheelView实现上下滑动选择器
- 爬取大学公告信息 beautifulsoup的使用
- UIButton
- js 利用canvas标签画一个存储按钮(附加svg动态按钮)
- Android中的MVP
- php正则应用:异常Delimiter must not be alphanumeric or backslash
- Sys::AlarmCall
- 【BZOJ 2002】 [Hnoi2010]Bounce 弹飞绵羊 LCT
- linux C语言之called object ‘maze’ is not a function or function pointer printf("%d\t", maze(i, j))
- Shell 脚本实践
- Android多屏幕适配-平板