This creates a rounded rectangle button UIButton *button1 = [UIButton buttonwithtype:uibuttontyperoundedrect]; The following 6 kinds of button types can be defined,//typedef enum {//Uibuttontypecustom = 0, custom style//uibuttontyperounded Rect, rounded rectangle//uibuttontypedetaildisclosure, Blue small arrow button, mainly to do detailed description with//Uibuttontypeinfolight, Bright Color exclamation point//Uibuttontypeinfodark, dark exclamation point//Uibuttontypecontactadd, Cross plus button//} Uibuttontype; The position of the given button on the view Button1.frame = CGRectMake (20, 20, 280, 20); Button background color button1.backgroundcolor = [Uicolor Clearcolor]; Set button fill picture//[button1 setimage:[uiimage imagenamed:@ "Btng.png"] forstate:uicontrolstatenormal]; Set button title [Button1 settitle:@ "click" Forstate:uicontrolstatenormal]; /* Forstate: The function of this parameter is to define the text of the button or the picture in which state will appear *////The following are several states//enum {//UIControlStateNormal = 0, general status Show//uicontrolstatehighlighted = 1 << 0, highlight State displayNow//uicontrolstatedisabled = 1 << 1, the disabled state will only show//uicontrolstateselected = 1 << 2, Checked status//Uicontrolstateapplication = 0x00ff0000 when application flag//Uicontrolstatereser ved = 0xff000000 is reserved for the internal frame and can be used regardless of his//}; /* * By default, when the button is highlighted, the color of the image will be painted darker, if this property below is set to NO, * then this function can be removed * * button1.adjustsimagewhenhighlighted = NO; /* As in the above case, by default, when the button is disabled, the image will be drawn darker, set NO to cancel the setting */button1.adjustsimagewhendisabled = NO; /* This property is set to YES in the state, the button will glow */button1.showstouchwhenhighlighted = yes; /* Add an event to the button, there are a number of events, I will open a separate blog to introduce them, the following time means to press the button, and the finger off the screen when the event triggered, and the Web as the Click event. After triggering this event, execute Butclick: This method, addtarget:self, means that this method can also pass in the pointer of another class in this class */[Button1 addtarget:self Action: @selector (BUTC Lick:) forcontrolevents:uicontroleventtouchupinside];
Settings for buttons in normal and highlighted colors
[_button1 Settitlecolor:[uicolor Yellowcolor] forstate:uicontrolstatehighlighted]; When the button is pressed down, the whole button will turn yellow.
[_button1 settitlecolor:[uicolor Bluecolor] forstate:uicontrolstatenormal];//button in normal state, the caption text on the button is set to Blue
Display Controls [Self.view Addsubview:button1];
iOS Development UIButton Detailed