(i) General operation
1. Define a button named BTN, horizontal axis 10, ordinate 0, Width 100, height 30
UIButton *btn = [[UIButton alloc] Initwithframe:cgrectmake (0)];
2. Set the button name: leaderboard;
Forstate: The function of this parameter is to define how the text or picture of the button will appear in the state.
[BTN Settitle:@ " leaderboard " Forstate:uicontrolstatenormal];
3. Add processing events for the button: Handle event method Name: onbtnclicked
Addtarget:self means that this method can also pass pointers to other classes in this class.
[Btn addtarget:self Action: @selector (onbtnclicked:) forcontrolevents:uicontroleventtouchupinside];
About the forControlEvents parameter:
Uicontroleventtouchdown Single Touch Press event: The user touches the screen, or when a new finger falls uicontroleventtouchdownrepeat Multi-touch press event, touch count Greater than 1: when the user presses the second to third or fourth finger. Uicontroleventtouchdraginside when a touch is dragged within the control window. Uicontroleventtouchdragoutside when a touch is dragged outside the control window. Uicontroleventtouchdragenter when a touch is dragged from outside the control window to the inside. Uicontroleventtouchdragexit when a touch is dragged from inside the control window to the outside. UIControlEventTouchUpInside all touch lift events within the control. Uicontroleventtouchupoutside all touch lift events outside the control (the touch must start with the inside of the control to send a notification). Uicontroleventtouchcancel all touch cancellation events, that is, one touch is canceled because it has too many fingers, or is locked or interrupted by a phone call. Uicontroleventtouchchanged a notification is sent when the value of the control changes. Used for sliders, segmented controls, and other controls that take values. You can configure when a slider control sends notifications when the slider is dropped, or when it is dragged. Uicontroleventeditingdidbegin a notification is sent when the text control starts editing. Uicontroleventeditingchanged sends a notification when text in a text control is changed. Uicontroleventeditingdidend a notification is sent when the edit ends in a text control. Uicontroleventeditingdidonexit sends a notification when the edit is finished by pressing the ENTER key (or equivalent behavior) within the text control. Uicontroleventalltouchevents notifies all touch events. UicontrOleventalleditingevents notifies all events about text editing. Uicontroleventallevents
Notifies all events.
4. Set the button name font size, color: size 15, color: Black
[Btn.titlelabel setfont:[uifont systemfontofsize:]; [btn Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];
5. Setting the Border
[Btn.layer Setcornerradius:10.0]; [Btn.layer setborderwidth: 1.0 ]; [Btn.layer Setbordercolor:[uicolor Redcolor]. Cgcolor];
First set the border style to: Rounded rectangle, and fillet radius of 10
Then set the border width to 1
Then set the border color to: red
Note: Another way to set the border color
Cgcolorspaceref colorspace = Cgcolorspacecreatedevicergb (); 0 0 0 1 }); [Btn.layer Setbordercolor:colorref]; //
6. Add a button to the view
[Viewson.view ADDSUBVIEW:BTN];
Viewson is the view view of the button to be added
(ii) Event handling
In console output: This is the BTN button
-(void) onbtnclicked: (uibutton*) button{NSLog ("This is the btn button");}
Note: With the above creation in the same viewcontroller
iOS code layout (ii) button