iOS development to customize a control by code

Source: Internet
Author: User

About inheritance relationships for controls (interview highlights):

( 1 all controls inherit from the UIView .

( 2 ) can listen to events are inherited from the first Uicontrol , Uicontrol again inherit from UIView . Like UIButton.

( 3 ) can be sliding in the whole is inherited from Uiscrollview , Uiscrollview again inherit from UIView . Like UITableView.

( 4 ) UIWindow is also inherited from UIView .

First, the code to create the control is written in

-(void) viewdidload{

};

, this method is the method that executes after the view has been created.

1. Create a button

UIButton *btn = [[UIButton alloc] init];

2. Add a button to the view

[Self.view ADDSUBVIEW:BTN];

3. Set frame (sets the position and size of the control)

Btn.frame = CGRectMake (50, 50, 100, 100);

4. Set the background image

(1) Loading images by file name (all PNG images, do not add extended name)

UIImage *normal = [UIImage imagenamed:@ "btn_01"];

(2) Set background image in normal state

[Btn Setbackgroundimage:normal Forstate:uicontrolstatenormal];

(3) Load the highlighted picture

UIImage *high = [UIImage imagenamed:@ "btn_02"];

[Btn Setbackgroundimage:high forstate:uicontrolstatehighlighted];

5. Set text

[Btn settitle:@ "point me," forstate:uicontrolstatenormal];

[Btn settitle:@ "Touch me what to do" forstate:uicontrolstatehighlighted];

6. Set Text color

[btn Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatenormal];

[btn Settitlecolor:[uicolor Bluecolor] forstate:

Uicontrolstatehighlighted];

7. Tap the Monitor button

[btn addTarget: Self action: @selector (btnclick:) forcontrolevents:

UIControlEventTouchUpInside];

"Remarks" * A control can be found by tag, for example:

UIButton *button = (UIButton *) [Self.view viewwithtag:10];

Because the return value of [Self.view Viewwithtag:10] is uiview, it needs to be cast to UIButton (UIButton *).

* For listening button click

[Btn addtarget:self Action: @selector (Btnclick:) forControlEvents:

UIControlEventTouchUpInside];

The Addtarget parameter represents which view,action represents which method in this view.

iOS development to customize a control by code

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.