Swift-按钮(UIButton)详解//声明按钮的⼀个对象,是全局的
var button1 :UIButton!
overridefunc viewDidLoad() {
super.viewDidLoad()
//以⾃定义类型创建按钮
button1 =UIButton.init(type:UIButtonType.Custom)
//也可以这样来创建按钮
// let button2 = UIButton(type:.Custom)
// let button3 = UIButton(type:.Custom)
// let button4 = UIButton.init(frame: CGRectMake(10, 10, 100, 100))
//另外,按钮也可以⽤以下类型创建
/
/默认⽂字颜⾊为蓝⾊,不带图标
button1 =UIButton(type:.System)
阿拉伯狼
//默认⽂字颜⾊为蓝⾊,带“!”图标按钮
button1 =UIButton(type:.DetailDisclosure)
//感叹号“!”圆形按钮
button1 =UIButton(type:.InfoLight)
//感叹号“!"圆形按钮
button1 =UIButton(type:.InfoDark)
//默认⽂字为蓝⾊,带“+”图标按钮
button1 =UIButton(type:.ContactAdd)
主题餐厅
//设置按钮的尺⼨
button1.frame =CGRectMake(100,100, 100,100)
//设置按钮的背景⾊
彩云之南歌词
button1.backgroundColor =UIColor.whiteColor()
button1.backgroundColor =UIColor.init(white:0.9, alpha: 1)
//设置按钮的背景图⽚
梦见电线着火//普通状态
button1.tBackgroundImage(UIImage(named:"icon1"), forState: UIControlState.Normal)
//⾼亮状态
button1.tBackgroundImage(UIImage(named:"icon2"), forState: UIControlState.Highlighted) //选中状态
button1.tBackgroundImage(UIImage(named:"icon3"), forState: UIControlState.Selected)
//设置按钮的⽂字和⽂字的颜⾊
//普通状态
button1.tTitle("普通", forState: UIControlState.Normal)
button1.tTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
//⾼亮状态
button1.tTitle("⾼亮", forState: UIControlState.Highlighted)
button1.dColor(), forState: UIControlState.Highlighted)
评课稿//选中状态
button1.tTitle("选中", forState: UIControlState.Selected)
button1.tTitleColor(UIColor.blackColor(), forState: UIControlState.Selected)
//设置按钮不同状态下的图⽚
//普通状态
button1.tImage(UIImage(named:"icon1"), forState: UIControlState.Normal)
//⾼亮状态
button1.tImage(UIImage(named:"icon2"), forState: UIControlState.Highlighted)
//选中状态
button1.tImage(UIImage(named:"icon3"), forState: UIControlState.Selected)
//设置按钮上⽂字的阴影
//普通状态下
button1.Color(), forState: UIControlState.Normal)
//⾼亮状态下
button1.llowColor(), forState: UIControlState.Highlighted)
//选中状态下
button1.tTitleShadowColor(UIColor.purpleColor(), forState: UIControlState.Selected)
//设置tag值
button1.addTarget(lf, action:#lector(buttonClick(_:)), forControlEvents:UIControlEvents.TouchUpInside)
电脑登录密码怎么设置//按钮内容的边距(顶部,左边,底部,左边)
//按钮上图⽚的边距
button1.imageEdgeInts =UIEdgeIntsMake(10,0, 20,0)
//按钮上⽂本框的边距
button1.titleEdgeInts =UIEdgeIntsMake(5,0, 10,0)
//按钮长按状态下去掉⽂字阴影
//⾼亮状态下调整图⽚
button1.adjustsImageWhenHighlighted =true
//⾼亮状态下变灰
button1.showsTouchWhenHighlighted =true
//设置按钮上的⽂字
button1.titleLabel?.text ="请您关注多多关注"
//设置按钮上的图⽚
button1.imageView?.image =UIImage.init(named:"icon1")
//添加按钮
lf.view.addSubview(button1)
}
/
/按钮的点击事件的处理
func buttonClick(button :UIButton){
//反选效果
button.lected = !button.lected;
//获取按钮当前⽂字
print(button.currentTitle)
/*
按钮当前的⼀些属性,注意:只能读取,不能修改
currentTitle//当前⽂字
currentTitleColor//当前⽂字颜⾊
currentTitleShadowColor//当前⽂字阴影⾊
currentImage//当前图⽚
男性壮阳currentBackgroundImage//当前背景图⽚
*/
}
表白失败