1. The creation of the button. 1. There are four types of buttons:
uibuttontype . Contactadd: Front with "+" icon button, default text color is blue, with touch highlighting effect uibuttontype uibuttontypeuibuttontype uibuttontype uibuttontype
//创建一个ContactAdd类型的按钮let button:UIButton = UIButton(type:.ContactAdd)//设置按钮位置和大小button.frame=CGRectMake(10, 150, 100, 30)//设置按钮文字button.setTitle("按钮", forState:UIControlState.Normal)self.view.addSubview(button);
2. For the Custom Custom type button, the code can be simplified to:
let button = UIButton(frame:CGRectMake(10, 150, 100, 30))
3. Text setting of the button
button.setTitle("普通状态", forState:UIControlState.Normal) //普通状态下的文字button.setTitle("触摸状态", forState:UIControlState.Highlighted) //触摸状态下的文字button.setTitle("禁用状态", forState:UIControlState.Disabled) //禁用状态下的文字
4. Setting the button text color
button.setTitleColor(UIColor.blackColor(),forState: .Normal) //普通状态下文字的颜色button.setTitleColor(UIColor.greenColor(),forState: .Highlighted) //触摸状态下文字的颜色button.setTitleColor(UIColor.grayColor(),forState: .Disabled) //禁用状态下文字的颜色
5. Setting the color of the button text shadow
button.setTitleShadowColor(UIColor.greenColor(),forState:.Normal) //普通状态下文字阴影的颜色button.setTitleShadowColor(UIColor.yellowColor(),forState:.Highlighted) //普通状态下文字阴影的颜色button.setTitleShadowColor(UIColor.grayColor(),forState:.Disabled) //普通状态下文字阴影的颜色
6. button background Color setting
button.backgroundColor=UIColor.blackColor()
7. Setting of the button text icon
button.setImage(UIImage(named:"icon1"),forState:.Normal) //设置图标button.adjustsImageWhenHighlighted=false //使触摸模式下按钮也不会变暗button.adjustsImageWhenDisabled=false //使禁用模式下按钮也不会变暗
8. Set the button background image
button.setBackgroundImage(UIImage(named:"background1"),forState:.Normal)
9. Button Touch Click event Response
//do not pass touch objects (i.e. clicked button) Button.addtarget (touchupinside) func tapped () {print ( "tapped")}//passes the touch object (that is, the button clicked), you need to define the action parameter, with the method name followed by a colon button.addtarget (self, Action: #selector (tapped (_:)), Forcontrolevents:. touchupinside) func tappedprint (button.titleforstate (.
Common types of touch events:
TouchDown:单点触摸按下事件,点触屏幕TouchDownRepeat:多点触摸按下事件,点触计数大于1,按下第2、3或第4根手指的时候TouchDragInside:触摸在控件内拖动时TouchDragOutside:触摸在控件外拖动时TouchDragEnter:触摸从控件之外拖动到内部时TouchDragExit:触摸从控件内部拖动到外部时TouchUpInside:在控件之内触摸并抬起事件TouchUpOutside:在控件之外触摸抬起事件TouchCancel:触摸取消事件,即一次触摸因为放上太多手指而被取消,或者电话打断
Richard_yu
Links: http://www.jianshu.com/p/310e3c3d00cd
Source: Pinterest
Copyright belongs to the author.
Use of Swift-button (UIButton)