IOS basic learning UIButton Usage Details

Source: Internet
Author: User

IOS basic learning UIButton Usage Details

The UIButton button is the most commonly used control in IOS development. As a basic knowledge of IOS learning, beginners need to understand its basic definition and common settings so that they can be used in development.

1. UIButton Definition

UIButton * button = [[UIButton buttonWithType :( UIButtonType );

The following six types of buttons can be defined,

Typedef enum {

UIButtonTypeCustom = 0, custom Style

UIButtonTypeRoundedRect, rounded rectangle

UIButtonTypeDetailDisclosure, blue arrow button, mainly used for detailed description

UIButtonTypeInfoLight, brilliant exclamation point

UIButtonTypeInfoDark, dark exclamation point

UIButtonTypeContactAdd, plus sign

} UIButtonType;

2. Set frame

Button1.frame = CGRectMake (20, 20,280, 40 );

[Button setFrame: CGRectMake (20, 20, 50, 50)];

Third, button background color

Button1.backgroundColor = [UIColor clearColor];

[Button setBackgroundColor: [UIColor blueColor];

4. state

ForState: this parameter defines the status in which the text or image of a button appears.

Enum {

UIControlStateNormal = 0, the general status is displayed

UIControlStateHighlighted = 1 <0, highlighted

UIControlStateDisabled = 1 <1, the disabled status is displayed.

UIControlStateSelected = 1 <2, selected

UIControlStateApplication = 0x00FF0000, when the application flag

UIControlStateReserved = 0xFF000000 is reserved for the internal framework.

};

@ Property (nonatomic, getter = isEnabled) BOOL enabled; // default is YES. if NO, ignores touch events and subclasses may draw differently

@ Property (nonatomic, getter = isSelected) BOOL selected; // default is NO may be used by some subclasses or by application

@ Property (nonatomic, getter = isHighlighted) BOOL highlighted;

Fifth, set the button to fill in the image and background image

[ButtonsetImage: [UIImageimageNamed: @ "checkmarkControllerIcon"] forState: UIControlStateNormal];

[ButtonsetBackgroundImage: [UIImageimageNamed: @ "checkmarkControllerIcon"] forState: UIControlStateNormal];

6. Set the button title and title Color

[Button1 setTitle: @ "click" forState: UIControlStateNormal];

[ButtonsetTitleColor: [UIColorredColor] forState: UIControlStateNormal];

Seventh, the setting button will shine

Button. showsTouchWhenHighlighted = NO;

8. add or delete events

[Button1 addTarget: self action: @ selector (butClick :) forControlEvents: UIControlEventTouchUpInside];

[Btn removeTarget: nil action: nil forControlEvents: UIControlEventTouchUpInside];

9. Set the image spacing and title spacing inside the button.

UIEdgeInsets insets; // you can specify the image spacing in the buttons.

Insets. top = insets. bottom = insets. right = insets. left = 10;

Bt. contentEdgeInsets = insets;

Bt. titleEdgeInsets = insets; // Title spacing

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.