1. Create
1 @property (nonatomic, strong) UIButton *btn;
1 //1. Infrequently used2SELF.BTN = [[UIButton alloc]initwithframe:cgrectmake ( -, -, $, -)];3 //2. This method is commonly used (the above kind can be discarded)4SELF.BTN =[UIButton Buttonwithtype:uibuttontypecustom];5 /*6 uibuttontypecustom = 0,//Custom Type7 Uibuttontypesystem Ns_enum_available_ios (7_0),//system type8 9 uibuttontypedetaildisclosure,//Detailed description style (add I in the middle of the circle)Ten detailed description style for uibuttontypeinfolight,//light One uibuttontypeinfodark,//detailed description style for dark A uibuttontypecontactadd,//plus Style - - uibuttontyperoundedrect = uibuttontypesystem,//Rounded Rectangle the }; - - */
2. Set the size
_btn.frame = CGRectMake (a);
3. Setting the background
1 // Set Background color 2 [self.btn setbackgroundcolor:[uicolor orangecolor]; 3 // set a background picture 4 [Self.btn setbackgroundimage:[uiimage imagenamed:@ "bg"] forstate: UIControlStateNormal];
4. Setting the title
/* Note: The first four use a little more uicontrolstatenormal //Normal (this use the most) uicontrolstatehighlighted // In the highlighted state (when the button is not lifted) uicontrolstatedisabled// disabled uicontrolstateselected //checked state Uicontrolstateapplication //uicontrolstatereserved// reserved for internal frames when application flags * / [_btn settitle:@ " edit " forstate:uicontrolstatenormal]; // set the title color [_btn settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];
5. Add an event to the button
// if the event has parameters that can only be UIButton type // when an event of the button is triggered, the system passes the button object as a parameter to the event /* Uicontroleventtouchdown Press down will respond to the uicontroleventtouchupoutside point is not reflected in the range to let go of the response (generally use this) * /[_btn addtarget:self Action: @selector (Changtitle) forControlEvents: Uicontroleventtouchdown]; [_btn addtarget:self Action: @selector (Changtitle) forcontrolevents:uicontroleventtouchupoutside];
How to change the caption of a button (3)
- (void) Changtitle: (UIButton *) sender{//Change the caption of a button (there are three methods)//1.property Properties: _btn or self.btn access//Get button title if([_btn.titlelabel.text isequaltostring:@"Edit"]) {[Self.btn settitle:@"Complete"Forstate:uicontrolstatenormal]; } Else{[self.btn settitle:@"Edit"Forstate:uicontrolstatenormal]; }; //2.tag ValueUIButton *button = [Self.view viewwithtag:1]; //3. Pass directly as a parameter (this method is used more)//addtarger}
Use of iOS UIButton