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