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