Reprint please declare source: http://blog.csdn.net/jinnchang/article/details/44403537
1. UIButton Overview
Inheritance: UIButton, Uicontrol, UIView
Control style:
2. Initialization of UIButton(1) using the Buttonwithtype Build button, there are six types of the following:
Enum Uibuttontype:int {case custom//custom style case system//Rounded rectangle case detaildisclosure//Blue small arrow case Infol ight//Bright exclamation case infodark//Dark exclamation case contactadd//cross plus}
(2) Using the Frame custom button
3. Examples of Use
Override Func Viewdidload () {///1, use the existing Type Build button let Commonbutton = Uibutton.buttonwithtype (. System) as UIButton//Modify button position and size Commonbutton.frame = CGRectMake (SELF.VIEW.FRAME.WIDTH/2-100, 100, 200, 200)// Set button background picture Commonbutton.setbackgroundimage (UIImage (named: "Logo.jpg"), ForState:UIControlState.Normal)//Add Click event C Ommonbutton.addtarget (Self, Action: "Buttonactions:", forControlEvents:UIControlEvents.TouchUpInside)//Set button label com Monbutton.tag = 1//2, custom button Let CustomButton = UIButton (Frame:cgrectmake (SELF.VIEW.FRAME.WIDTH/2-100, 400, 2 00, 200))//Set button caption Custombutton.settitle ("Custom", ForState:UIControlState.Normal)//Set button caption color CustomButton. Settitlecolor (Uicolor.redcolor (), forState:UIControlState.Normal)//Set Button caption Shadow Custombutton.settitleshadowcolor (UIC Olor.blackcolor (), forState:UIControlState.Normal)//Set button Shadow Custombutton.titlelabel?. Shadowoffset = Cgsizemake (1.0, 1.0)//Set button caption font style Custombutton.titlelabel!. Font = uifont.systemfontofsize (18)//Set button title wrap mode custombutton.titlelabel!. Linebreakmode =. Bytruncatingtail//Set button background color Custombutton.backgroundcolor = Uicolor (red:0.8,green:0.8,blue:0.8,alpha:1.0)//Set button inside Part content Margin Custombutton.contentedgeinsets = uiedgeinsetsmake (-100, 0, 0, 0)//Remove the image color burn in the highlighted state Custombutton.adjustsimag ewhenhighlighted = false; Remove Image Color Burn custombutton.adjustsimagewhendisabled = false in disabled state; Add a button to press the Glow effect custombutton.showstouchwhenhighlighted = true; Add Click event Custombutton.addtarget (self,action: "Buttonactions:", forControlEvents:UIControlEvents.TouchUpInside)// Set Button Label Custombutton.tag = 2 Self.view.addSubview (Commonbutton) Self.view.addSubview (CustomButton)}///response button click Thing Piece func buttonactions (sender:uibutton!) {println (Sender.tag)}
4, ForstateThe function of this parameter is to define the state in which the text or picture of the button will appear. There are several states:
- normal (normal state)
- highlighted ( pressed state)
- disabled ( disabled state)
- Selected ( selected (finger has left)
- application ( Span style= "White-space:pre" > application flags)
- reserved ( reserved state)
5. The difference between two ways to set the background image
- Setbackgroudimage: Picture will be stretched
- SetImage: Picture remains the original size
6. ConclusionProject on Github address: Uibuttonsample Article last updated: March 18, 2015 10:11:03. the resources are as follows:
(1) UIButton Class Reference
(2) UIKit User Interface catalog:buttons
Introduction to Swift Development: button (UIButton)