Use of iOS UIButton

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.