iOS Basic Learning UIButton use detailed

Source: Internet
Author: User
Tags bool

The UIButton button is the most commonly used control in iOS development, and as a basic learning tutorial for iOS, beginners need to understand their basic definitions and common settings to be proficient in development.

First, the definition of UIButton

UIButton *button=[[uibutton Buttonwithtype: (Uibuttontype);

There are 6 kinds of button types that can be defined,

typedef enum {

Uibuttontypecustom = 0, custom style

Uibuttontyperoundedrect Rectangle with rounded corners

Uibuttontypedetaildisclosure, Blue small arrow button, mainly to do detailed instructions with

Uibuttontypeinfolight, Bright exclamation

Uibuttontypeinfodark, Dark exclamation

Uibuttontypecontactadd, Cross plus button

} Uibuttontype;

Second, set frame

Button1.frame = CGRectMake (20, 20, 280, 40);

[Button Setframe:cgrectmake (20,20,50,50)];

Third, button background color

Button1.backgroundcolor = [Uicolor Clearcolor];

[Button Setbackgroundcolor:[uicolor Bluecolor]];

Iv. State

Forstate: The function of this parameter is to define what state the button text or picture will appear

enum {

UIControlStateNormal = 0, normal state appears

uicontrolstatehighlighted = 1 << 0, highlight status appears

uicontrolstatedisabled = 1 << 1, disabled status will not appear

uicontrolstateselected = 1 << 2, checked status

Uicontrolstateapplication = 0x00ff0000, when application flags

uicontrolstatereserved = 0xff000000 reserved for internal frames, you can ignore

};

@property (nonatomic,getter=isenabled) BOOL enabled; The default is YES. If NO, ignores touch events and subclasses may draw differently

@property (nonatomic,getter=isselected) BOOL selected; Default is NO would used by some subclasses or by application

@property (nonatomic,getter=ishighlighted) BOOL highlighted;

Five, set button fill picture and background picture

[buttonsetimage:[uiimageimagenamed:@ "Checkmarkcontrollericon"]forstate:uicontrolstatenormal];

[buttonsetbackgroundimage:[uiimageimagenamed:@ "Checkmarkcontrollericon"]forstate:uicontrolstatenormal];

Set the button title and title color

[Button1 settitle:@ "click" Forstate:uicontrolstatenormal];

[Buttonsettitlecolor:[uicolorredcolor]forstate:uicontrolstatenormal];

The Set button will glow.

Button.showstouchwhenhighlighted=no;

Eighth, add or remove event handling

[Button1 addtarget:self Action: @selector (Butclick:) forcontrolevents:uicontroleventtouchupinside];

[Btn Removetarget:nil Action:nil forcontrolevents:uicontroleventtouchupinside];

Ninth, set the inner picture spacing and caption Spacing of the button

Uiedgeinsets insets; Set picture spacing inside a button

Insets.top = Insets.bottom = Insets.right = Insets.left = 10;

Bt.contentedgeinsets = insets;

Bt.titleedgeinsets = insets; Title Spacing

Related Article

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.