iOS Basic Learning -2:uibutton

Source: Internet
Author: User
Tags uicontrol

UIButton is a standard Uicontrol control, Uikit provides a set of controls: Uiswitch switch, UIButton button, Uisegmentedcontrol segmented control, UISlider slider, Uitextfield The Text field control, Uipagecontrol the paging control. The base classes for these controls are Uicontrol, and Uicontrol derive from the UIView class, so each control has many views of its own, including the ability to attach to other views. All controls have a common set of properties and methods.

The specific view relationships are as follows

1. Create Button 1.1 initWithFrame
UIButton *btn1 = [[UIButton alloc]initwithframe:cgrectmake];
Class 1.2 Method Buttonwithtype

Custom methods are often used, which is a more flexible approach

[UIButton Buttonwithtype:uibuttontypecustom];

and Uibuttontypecustom equivalent to the following ways:

enum {      0,               //    uibuttontyperoundedrect,              // white rounded Rectangle, Similar Preferences table unit or Address Book Card      uibuttontypedetaildisclosure,    // Blue disclosure button, can be placed next  to any text     uibuttontypeinfolight,        // Small Circle Info button, can be placed  next    to any text Uibuttontypeinfodark,        // white background using the Dark Circle Info button      uibuttontypecontactadd,        // The Blue Plus (+) button, which can be placed next  to any text } Uibuttontype;
2. Set Button Properties 2.1 Frame Property
Btn.frame = CGRectMake (10.010.060.044.0);  // x, y, weight, height;

Or:

[Btn Setframe:cgrectmake (+, +,+)];
2.2 Setting the title
[BTN1 settitle:@ " click " Forstate:uicontrolstatenormal];  // Set Title // set title color [btn1 settitleshadowcolor:[uicolor graycolor] forstate:uicontrolstatenormal]; // title Shadow color
2.3 Setting a background image
[BTN1 setimage:[uiimageimagenamed:@ "btn1img"] forstate:uicontrolstatenormal];   // button Picture [BTN1 setbackgroundimage:[uiimageimagenamed:@ "btn1bgimg"] forstate: UIControlStateNormal];  // Background Image
2.4 Setting the button picture interior spacing
Uiedgeinsets insets;     // set the internal picture spacing of the button Ten  = insets;    // content Spacing     Bt.titleedgeinsets = insets;         // title Spacing

3. Button State 3.1 common status

After Forstate, can choose the following States, commonly used include: normal, highlighting, disabling, selected;

enum{UIControlStateNormal=0,//Normaluicontrolstatehighlighted =1<<0,//Highlightuicontrolstatedisabled =1<<1,//Disableduicontrolstateselected =1<<2,//selectedUicontrolstateapplication =0x00ff0000,//When application flags are useduicontrolstatereserved =0xff000000          //reserved for the internal framework}; typedef nsuinteger Uicontrolstate;
3.2 Fine-tune properties when highlighted or disabled

When highlighted, the image color will deepen, if you want to turn this property off, set adjustsimagewhenhighlighted to No

btn1.adjustsimagewhenhighlighted = NO;

When off, the image color will be lighter, if you want to turn this property off, set adjustsimagewhendisabled to No

btn1.adjustsimagewhendisabled = NO;

When selected, you can set the button glow, set showstouchwhenhighlighted to Yes

btn1.showstouchwhenhighlighted = YES;
4. Add an action
[Btn1 addtarget:self Action: @selector (btnpressed:) forcontrolevents:uicontroleventtouchupinside];   // Add property to button's behavior -(void) btnpressed: (ID) sender{              // handle button press event    uibutton* btn = (uibutton*) sender;       if (BTN = =  1 {    NSLog (@ "btn1 pressed");}    }
5. Example

See the example below.

iOS Basic Learning -2: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.