IOS basics-2: UIButton, ios basics-2 uibutton

Source: Internet
Author: User
Tags uicontrol

IOS basics-2: UIButton, ios basics-2 uibutton

UIButton is a standard UIControl control. UIKit provides a set of controls: UISwitch switch, UIButton button, UISegmentedControl, UISlider slider, UITextField text field control, and UIPageControl paging control. The base classes of these controls are UIControl, and UIControl is derived from the UIView class. Therefore, each control has many view features, including the ability to attach to other views. All controls have a set of common attributes and methods.

The specific view relationships are as follows:

1. Create the button 1.1 initWithFrame
UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 80, 44)];
1.2 class method buttonWithType

The custom method is usually used, which is more flexible.

[UIButton buttonWithType:UIButtonTypeCustom];

This is equivalent to UIButtonTypeCustom in the following ways:

Typedef enum {UIButtonTypeCustom = 0, // custom, commonly used mode UIButtonTypeRoundedRect, // white rounded rectangle, similar to setting table unit or address book card secret, // blue disclosure button, you can place the UIButtonTypeInfoLight button next to any text, // a small circle information button, and a dark circle Information Button UIButtonTypeInfoDark next to any text. // The UIButtonTypeContactAdd button is used on a white background, // The Blue plus sign (+) button can be placed next to any text} UIButtonType;
2. Set the button attribute 2.1 Frame attribute
btn.frame = CGRectMake(10.0, 10.0, 60.0, 44.0);  //x, y , weight, height;

Or:

[btn setFrame:CGRectMake(20,20,50,50)];
2.2 set the title
[Btn1 setTitle: @ "click" forState: UIControlStateNormal]; // set the title [btn1 setTitleColor: [UIColor orangeColor] forState: UIControlStateNormal]; // set the title Color [btn1 setTitleShadowColor: [UIColor grayColor] forState: UIControlStateNormal]; // Title shadow color
2.3 set the background image
 
[Btn1 setImage: [UIImageimageNamed: @ "btn1Img"] forState: UIControlStateNormal]; // button image [btn1 success: [UIImageimageNamed: @ "success"] forState: UIControlStateNormal]; // background image
2.4 set the internal spacing of the button Image
UIEdgeInsets insets; // you can specify the insets of the image spacing in the buttons. top = insets. bottom = insets. right = insets. left = 10; btn. contentEdgeInsets = insets; // content spacing bt. titleEdgeInsets = insets; // Title spacing

 

3. Button status 3.1 Common status

After forState, you can select the following states: Normal, highlighted, disabled, and selected;

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;
3.2 fine-tune attributes when highlighted or disabled

The color of the image increases when the image is highlighted. To disable this attribute, set adjustsImageWhenHighlighted to NO.

btn1.adjustsImageWhenHighlighted = NO;

When disabled, the image color becomes lighter. To disable this attribute, set adjustsImageWhenDisabled to NO

btn1.adjustsImageWhenDisabled = NO;

When selected, you can set the button to emit light. Set showsTouchWhenHighlighted to YES.

btn1.showsTouchWhenHighlighted = YES;
4. Add actions
[Btn1 addTarget: self action: @ selector (btnPressed :) forControlEvents: UIControlEventTouchUpInside]; // adds a property for the button action-(void) btnPressed :( id) sender {// processing button press event UIButton * btn = (UIButton *) sender; if (btn = btn 1) {NSLog (@ "btn1 pressed ");}}
5. Instance

For details, see the example below.

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.