一、UIButtonType:按钮样式
typedef NS_ENUM(NSInteger, UIButtonType) {
UIButtonTypeCustom = 0, // no button type
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0), // standard system button
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
UIButtonTypePlain API_AVAILABLE(tvos(11.0)) API_UNAVAILABLE(ios, watchos), // standard system button without the blurred background view
UIButtonTypeRoundedRect = UIButtonTypeSystem // Deprecated, use UIButtonTypeSystem instead
};
二、UIControlState:按钮的常见状态
typedef NS_OPTIONS(NSUInteger, UIControlState) {
UIControlStateNormal = 0,
UIControlStateHighlighted = 1 << 0, // used when UIControl isHighlighted is set
UIControlStateDisabled = 1 << 1,
UIControlStateSelected = 1 << 2, // flag usable by app (see below)
UIControlStateFocused NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 3, // Applicable only when the screen supports focus
UIControlStateApplication = 0x00FF0000, // additional flags available for application use
UIControlStateReserved = 0xFF000000 // flags reserved for internal framework use
};
按钮的三种常见状态.png
(1)Normal:(Default)
普通状态,未被点击时的显示状态。
对应的枚举常量:UIControlStateNormal
(2)Highlighted:
高亮状态,按钮被按下去但手指为抬起时的显示状态
对应的枚举常量:UIControlStateHighlighted
(3)Disabled:
失效,不可被点击状态:属性enabled为NO
对应的枚举常量:UIControlStateDisabled
三、样式的使用
(1)Xib方式创建的button
System样式(1) System样式(2)默认样式为System样式,该样式下button的高亮状态显示是灰灰的;
(2)手写方式创建的button
默认为Custom样式,该样式下button的高亮状态显示是亮亮的;
//自定义button样式(以下两种自定义方式效果相同)
//UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//button.frame = CGRectMake(100, 100, 100, 100);
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
//为被点击时显示的背景图片
[button setBackgroundImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
//被点击(高亮状态)时显示的背景图片
[button setBackgroundImage:[UIImage imageNamed:@"add_highlighted"] forState:UIControlStateHighlighted];
//失效状态下显示的背景图片
[button setBackgroundImage:[UIImage imageNamed:@"add_disabled"] forState:UIControlStateDisabled];
[self.view addSubview:button];
Custom样式(1)
Custom样式(2)
网友评论