In real-world development, many times it is necessary to manually write code to create a button.
In the development process, not every time through the storyboard drag control to complete the UI interface, because storyboard above the interface is "fixed dead", and sometimes may be in the program to dynamically add some new controls to the interface, such as QQ, chat information, Someone sends out a message and then shows it dynamically. Therefore, you need to know how to add controls dynamically in code. In fact, the essence of storyboard is to turn to the corresponding code (XML file) based on the graphical interface description. Another reason is that some domestic companies, or some IOS-developed teams, do not use storyboards for UI design, so mastering is necessary.
//1. Create a custom buttonUIButton*BTN =[UIButton Buttonwithtype:uibuttontypecustom];//2. Add a button[Self.view addsubview:btn];//3. Setting the position and size of the buttonBtn.frame= CGRectMake ( -, -, -, -);//4. Tap the button click (The Btnclick method of self is called when the button is clicked)[btn addtarget:self Action: @selector (Btnclick) forcontrolevents:uicontroleventtouchupinside];//5. Set the properties of the button in the default state//5.1. Background of the default state[btn setbackgroundimage:[uiimage imagenamed:@"btn_01"] forstate:uicontrolstatenormal];//5.2. Default state of text[btn Settitle:@"dot me."Forstate:uicontrolstatenormal];//5.3. The text color of the default state[btn Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatenormal];//6. Set the properties of the button in the highlighted State//6.1. Background of the highlighted State[btn setbackgroundimage:[uiimage imagenamed:@"btn_02"] forstate:uicontrolstatehighlighted];//6.2. Text in highlighted state[btn Settitle:@"feel what I'm doing ."forstate:uicontrolstatehighlighted];//6.3. The text color of the highlighted state[btn Settitlecolor:[uicolor Bluecolor] forstate:uicontrolstatehighlighted];
a uicolor represents a color that can be obtained in many common colors by means of the Uicolor class method .+ (Uicolor *) blackcolor; //0.0 white black + (Uicolor *) darkgraycolor; //0.333 WH ITE dark grey + (Uicolor *) lightgraycolor; //0.667 white bright Grey + (Uicolor *) whitecolor; //1.0 White + (Uicolor *) graycolor; //0.5 Gray + (Uicolor *) redcolor; & nbsp; //1.0, 0.0, 0.0 RGB red + (Uicolor *) greencolor; //0.0, 1.0, 0 .0 RGB Green + (Uicolor *) bluecolor; //0.0, 0.0, 1.0 RGB Blue + (Uicolor *) cyancolor; & nbsp; //0.0, 1.0, 1.0 RGB Cyan + (Uicolor *) yellowcolor; //1.0, 1.0, 0.0 RG B Yellow + (Uicolor *) magentacolor; //1.0, 0.0, 1.0 RGB Magenta + (Uicolor *) orangecolor; 1.0, 0.5, 0.0 RGB orange + (Uicolor *) purplecolor; //0.5, 0.0, 0.5 RGB purple + (Uicolor *) browncolor;& nbsp; 0.6, 0.4, 0.2 RGB Brown + (Uicolor *) clearcolor; //0.0 white, 0.0 alpha clear color (empty color) a UIImage object represents a picture, generally through the imagenamed: method you can load a picture in a project by file name (PNG-formatted picture can omit the extension)
UIImage *image = [UIImage imagenamed:@ "btn_01"];
UIButton comes with a lot of different styles .To specify a button style while creating a button with code
UIButton *btn = [UIButton buttonwithtype:uibuttontypecustom];
Uibuttontypecustom: No type, The content of the button requires a custom UIBUTTONTYPEDETAILDISCLOSURE:UIBUTTONTYPEINFOLIGHT:UIBUTTONTYPEINFODARK:UIBUTTONTYPECONTACTADD: Finally, there are submissions:
[Self.view ADDSUBVIEW:BTN];
The button code is submitted to the view. Show
The above should be uniformly written in the Viewdidload object method:
//The view load completion needs to call the method, the inherited method, needs to call the parent class this method, must not lose//usually the initialization of the view controller is done here- (void) viewdidload{[Super Viewdidload]; //Create buttonUIButton *btn =[[UIButton alloc] init]; Btn.frame= CGRectMake ( -, -, $, $); Btn.backgroundcolor=[Uicolor Redcolor]; [Self.view ADDSUBVIEW:BTN]; [Btn addtarget:self Action: @selector (click:) forcontrolevents:uicontroleventtouchupinside];}- (void) Click: (ID) sender{NSLog (@"DDD");}
Addtarget to monitor settings, equivalent to the method in the storyboard, the connection, ibaction. If it is the connection of the control property, that is Iboutlet, the value can be assigned directly.
Summary:
To create a control using code, follow these steps:
1> creating an object using the control's corresponding class
2> Setting Object properties: Frame\color\text\image\backgroundimage ...
3> [Self.view addsubview:btn]; To add a control to a view
The sample code for setting the control listener method is as follows:
[btn AddTarget: SelfAction:@selector (click:) forcontrolevents:uicontroleventtouchupinside];
Tips:
The 1> addTarget method is defined in the Uicontrol class, which means that you can add a listening method to all objects that inherit from the Uicontrol class
The first parameter of the 2> listener method is the object itself
The second parameter of the 3> listener method is the event that listens to the control
Viewdidload is the method that is called after the view is loaded, usually in this method to perform the initialization of the view controller , in the Viewdidload method, do not forget to call the parent class method implementation!
[Super Viewdidload];
IOS Development Notes-Basic UI (5) Using Code creation buttons