Comprehensive Analysis of UIButton

Source: Internet
Author: User

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, // custom, no style
UIButtonTypeRoundedRect, // white rounded rectangle, similar to table unit or address book card preference
UIButtonTypeDetailDisclosure, // The Blue disclosure button, which can be placed beside any text
UIButtonTypeInfoLight, // the small circle information button used by the widget, which can be placed beside any text
UIButtonTypeInfoDark, // The dark circle information button used in the white background
UIButtonTypeContactAdd, // 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. 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 setBackgroundImage: [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, // highlight
UIControlStateDisabled = 1 <1, // disable
UIControlStateSelected = 1 <2, // select
UIControlStateApplication = 0x00FF0000, // when the application flag is used
UIControlStateReserved = 0xFF000000 // reserved for the 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:
 
[Btn1 addTarget: self action: @ selector (btnPressed :) forControlEvents: UIControlEventTouchUpInside];
 
-(Void) btnPressed :( id) sender {

UIButton * btn = (UIButton *) sender;
// Start writing your own actions
}

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.