Uibutton button control-iOS development

Source: Internet
Author: User
Tags uicontrol
Statement

You are welcome to repost this article, but please respect the author's Labor achievements. repost this article and keep the statement in this box. Thank you.
Article Source: http://blog.csdn.net/iukey

Uibutton is a standard uicontrol control. If you do not know much about uicontrol, read another blog article: uicontrol IOS control programming.

I. Create

Two methods:

1. Regular initwithframe

UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 80, 44)];

For details about how to create a view in code (uicontrol is inherited from uiview, so it is also view), see: several basic knowledge points about view.

2. A Class Method of uibutton (or static method) buttonwithtype

UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

The style is as follows:

Typedef Enum {uibuttontypecustom = 0, // No button type custom, no style uibuttontyperoundedrect, // rounded rect, flat white button, like in address card white rounded rectangle, similar to preference settings, the table unit or address book card uibuttontypedetaildisclosure, // The Blue disclosure button can be placed next to any text, and the tiny circle information button used by the uibuttontypeinfolight, // widget, you can put uibuttontypeinfodark next to any text, // The uibuttontypecontactadd button for the dark circle information used in the white background, // The Blue plus sign (+) button, which can be placed next to any text} uibuttontype;

Ii. Set attributes

1. Frame attributes

You can assign a value to the frame attribute of the button after creating a button in the following 2nd methods. You can use a cgrect structure to set the position and size of the button.

CGRect btn2Frame = CGRectMake(10.0, 10.0, 60.0, 44.0);    btn2.frame =btn2Frame;

2. Title attributes

You can set the button title in any specific status. You can set it using the settitle method:

[btn1 setTitle:@"BTN1" forState:UIControlStateNormal];

You can also set the status of a button to a graph. Use setimage:

[btn2 setImage:[UIImage imageNamed:@"pic"] forState:UIControlStateNormal];

In addition, you can set the title Color, shadow, and button background for each button status. Both settitlecolor and settitleshadowcolor require a uicolor object as a parameter:

[Btn1 settitlecolor: [uicolor redcolor] forstate: uicontrolstatenormal]; // set the title Color [btn1 settitleshadowcolor: [uicolor graycolor] forstate: uicontrolstatenormal]; // shadow [btn1 restart: [uiimage imagenamed: @ "pic"] forstate: uicontrolstatehighlighted]; // background image

The preceding methods all mention the common parameter forstate. This parameter determines the state in which the title, image, or other attributes will appear. You can program to make the button change in that status

Enum {uicontrolstatenormal = 0, // normal uicontrolstatehighlighted = 1 <0, // used when uicontrol ishighlighted is set highlight uicontrolstatedisabled = 1 <1, // disable uicontrolstateselected = 1 <2, // flag usable by APP (see below) Select uicontrolstateapplication = 0x00ff0000, // additional flags available for application use uicontrolstatereserved = 0xff000000 // flags reserved for internal framework use reserved for internal framework}; typedef nsuinteger uicontrolstate;

You only need to know the first four states.

When the button is highlighted or disabled, the uibutton class can adjust its own appearance. The following attributes allow you to fine-tune the appearance of the button as needed:

Adjustsimagewhenhighlighted

By default, when a button is disabled, the image will be painted in a darker color. To disable this function, set this attribute to no:

 btn1.adjustsImageWhenHighlighted = NO;

Adjustsimagewhendisabled

By default, when a button is disabled, the image is dimmed. To disable this function, set this attribute to no:

btn1.adjustsImageWhenDisabled = NO;

Showstouchwhenhighlighted

This
Set the property to yes to make the button glow when it is pressed. This can be used for information buttons or some important buttons:

btn1.showsTouchWhenHighlighted = YES;

3. display controls

The subsequent simplicity of the display control is as follows:

[self.view addSubview:btn1];    [self.view addSubview:btn2];

4. Rewrite the painting Behavior

You can use the subclass Button to customize your own button class. During subclass, You can reload the following methods, which return the cgrect structure and specify the boundary of each component of the button.

Note: Do not call these methods directly. These methods are written to the system call.

Backgroundrectforbounds // specifies the background boundary.
Contentrectforbounds // specify the content Boundary
Titlerectforcontentrect // specify the text title Boundary
Imagerectforcontentrect // specify the button Image Boundary

Example:

   - (CGRect)imageRectForContentRect:(CGRect)bounds{        return CGRectMake(0.0, 0.0, 44, 44);    }

5. Add actions

Why is the button used? Used to stimulate an action or event. Then we need to add an action for him, as described in uicontrol:

-(Void) btnpressed :( ID) sender {uibutton * BTN = (uibutton *) sender; // start to write your own action} [btn1 addtarget: Self action: @ selector (btnpressed :) forcontrolevents: uicontroleventtouchupinside];

Vi. End

End? It's not over yet, but uibutton is over. Let's leave another unfinished topic: uibarbuttonitem. What is the relationship between uibutton and uibutton? I will talk about it later. You just need to figure out uibutton.

A demo used to write this article is appended to the following: uibuttondemo

Related Article

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.