An explanation of the use of IOS UIButton

Source: Internet
Author: User

Button creation methods are:

  //Object Method InitializationUIButton *btn = [[UIButton alloc]initwithframe:cgrectmake ( -, -, -, the)]; //class method InitializationUIButton *BTN1 =[UIButton Buttonwithtype:uibuttontyperoundedrect]; //Set FrameBtn1.frame = CGRectMake ( -, -, -, the); Btn1.backgroundcolor= [Uicolor Blackcolor];//Set Background color[Btn1 Settitle:@"BTN1"Forstate:uicontrolstatenormal]; [Self.view addsubview:btn];//Add to view[Self.view ADDSUBVIEW:BTN1]; //Set Button caption[BTN Settitle:@"Button"Forstate:uicontrolstatenormal]; //Set highlight State[BTN Settitle:@"Highlight State"forstate:uicontrolstatehighlighted]; //A state of a button is set to a picture[Btn setimage:[uiimage imagenamed:@"pic"] Forstate:uicontrolstatenormal]; //set the title color[btn Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatenormal]; //Shadows[btn Settitleshadowcolor:[uicolor Graycolor] forstate:uicontrolstatenormal]; //set a background picture[Btn setbackgroundimage:[uiimage imagenamed:@"pic"] forstate:uicontrolstatehighlighted]; //Set Font sizeBtn.titleLabel.font = [Uifont fontwithname:@"btn"Size the]; //Set Tag Value[BTN Settag:101]; //set four fillet radiiBtn.layer.cornerRadius =4.5; //button Border WidthBtn.layer.borderWidth =0.5;

State status

State forstate: The function of this parameter is to define the state in which the text or picture of the button will appear.enum{UIControlStateNormal=0, the general state appears uicontrolstatehighlighted=1<<0, the highlight state appears uicontrolstatedisabled=1<<1, the disabled state will only appear uicontrolstateselected=1<<2, check state uicontrolstateapplication=0x00ff0000When the application flag is uicontrolstatereserved=0xff000000reserved for internal frames, can be whatever he};
//when the button is highlighted or disabled, the UIButton class can adjust its appearance, and the following properties allow you to fine-tune the appearance of the button as needed://adjustsimagewhenhighlighted//by default, when the button is disabled, the image is painted darker. To disable this feature, set this property to No:btn1.adjustsimagewhenhighlighted =NO; //adjustsimagewhendisabled//by default, when the button is disabled, the image will be painted in a lighter color. To disable this feature, set this property to No:btn1.adjustsimagewhendisabled =NO; //showstouchwhenhighlighted//This property is set to Yes to make the button glow when pressed. This can be used for informational buttons or some important buttons:btn1.showstouchwhenhighlighted = YES;

Rewrite drawing

//overriding paint Behavior//    //You can customize your own button class by using the Sub-type button. In subclasses you can overload the following methods, which return the CGRect structure, indicating the bounds of each component of the button. //Note: Do not call these methods directly, these methods are what you write to the system call. //Backgroundrectforbounds//Specify background bounds//Contentrectforbounds//Specify content Boundaries//Titlerectforcontentrect//Specify text caption Boundaries//Imagerectforcontentrect//Specify button image bounds
- (CGRect) Imagerectforcontentrect: (cgrect) bounds{    return cgrectmake (0.00.0  );} [Btn1 addtarget:self Action: @selector (btnpressed:) forcontrolevents:uicontroleventtouchupinside]; // Add Click Button Event -(void) btnpressed: (ID) sender{    UIButton* btn = (uibutton* ) sender;     // start writing your own action }
 //forcontrolevents parameter Typestypedef ns_options (Nsuinteger, uicontrolevents) {Uicontroleventtouchdown=1<<0,//Single Touch Press event: The user touches the screen, or when a new finger falls. Uicontroleventtouchdownrepeat =1<<1,//Multi-Touch press event, touch count greater than 1: when the user presses the second to third or fourth finger. Uicontroleventtouchdraginside =1<<2,//When a touch is dragged within the control window. Uicontroleventtouchdragoutside =1<<3,//When a touch is dragged outside the control window. Uicontroleventtouchdragenter =1<<4,//When a touch is dragged from outside the control window to the insideUicontroleventtouchdragexit =1<<5,//When a touch is dragged from inside the control window to the outside. UIControlEventTouchUpInside =1<<6,//all touch lift events within the controlUicontroleventtouchupoutside =1<<7,//all touch lift events outside the control (the touch must start with the inside of the control to send a notification). Uicontroleventtouchcancel =1<<8,//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. uicontroleventvaluechanged=1<< A,//sends a notification when the value of the control has changed. 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=1<< -,//send a notification when you start editing in a text controlUicontroleventeditingchanged =1<< -,//a notification is sent when the text in the text control is changed. Uicontroleventeditingdidend =1<< -,//sends a notification when the edit ends in a text control. Uicontroleventeditingdidendonexit =1<< +,//sends a notification when the edit is finished by pressing the ENTER key (or equivalent behavior) within the text control. uicontroleventalltouchevents=0x00000fff,//notifies all touch events. Uicontroleventalleditingevents =0x000f0000,//notifies all events about text editing. uicontroleventapplicationreserved =0x0f000000,//range available for application useuicontroleventsystemreserved =0xf0000000,//range reserved for internal framework useUicontroleventallevents =0xFFFFFFFF   //Notify all Events

An explanation of the 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.