02
|
UIButton
*button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
|
04
|
//
能够定义的button类型有以下6种,
|
06
|
//
UIButtonTypeCustom = 0, 自定义风格
|
07
|
//
UIButtonTypeRoundedRect, 圆角矩形
|
08
|
//
UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用
|
09
|
//
UIButtonTypeInfoLight, 亮色感叹号
|
10
|
//
UIButtonTypeInfoDark, 暗色感叹号
|
11
|
//
UIButtonTypeContactAdd, 十字加号按钮
|
15
|
button1.frame
= CGRectMake(20, 20, 280, 20);
|
18
|
button1.backgroundColor
= [UIColor clearColor];
|
21
|
//[button1
setImage:[UIImage imageNamed:@"btng.png"] forState:UIControlStateNormal];
|
24
|
[button1
setTitle:@ "点击" forState:UIControlStateNormal];
|
26
|
/*
forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现*/
|
29
|
//
UIControlStateNormal = 0, 常规状态显现
|
30
|
//
UIControlStateHighlighted = 1 << 0, 高亮状态显现
|
31
|
//
UIControlStateDisabled = 1 << 1, 禁用的状态才会显现
|
32
|
//
UIControlStateSelected = 1 << 2, 选中状态
|
33
|
//
UIControlStateApplication = 0x00FF0000, 当应用程序标志时
|
34
|
//
UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管他
|
38
|
*
默认情况下,当按钮高亮的情况下,图像的颜色会被画深一点,如果这下面的这个属性设置为no,
|
41
|
button1.adjustsImageWhenHighlighted
= NO;
|
42
|
/*跟上面的情况一样,默认情况下,当按钮禁用的时候,图像会被画得深一点,设置NO可以取消设置*/
|
43
|
button1.adjustsImageWhenDisabled
= NO;
|
44
|
/*
下面的这个属性设置为yes的状态下,按钮按下会发光*/
|
45
|
button1.showsTouchWhenHighlighted
= YES;
|
47
|
/*
给button添加事件,事件有很多种,我会单独开一篇博文介绍它们,下面这个时间的意思是
|
48
|
按下按钮,并且手指离开屏幕的时候触发这个事件,跟web中的click事件一样。
|
49
|
触发了这个事件以后,执行butClick:这个方法,addTarget:self
的意思是说,这个方法在本类中
|
51
|
[button1
addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
|
55
|
[self.view
addSubview:button1];
|
UIButtonType:
typedef enum {
UIButtonTypeCustom = 0, // no button type 自定义,无风格
UIButtonTypeRoundedRect, // rounded rect, flat white button, like in address card 白色圆角矩形,类似偏好设置表格单元或者地址簿卡片
UIButtonTypeDetailDisclosure,//蓝色的披露按钮,可放在任何文字旁
UIButtonTypeInfoLight,//微件(widget)使用的小圆圈信息按钮,可以放在任何文字旁
UIButtonTypeInfoDark,//白色背景下使用的深色圆圈信息按钮
UIButtonTypeContactAdd,//蓝色加号(+)按钮,可以放在任何文字旁
} UIButtonType;
UIButton常用属性:
//设置对应状态的标题内容default is nil. title is assumed to be single line
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
//设置对应状态的标题颜色
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
//设置对应状态的标题阴影颜色
- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state;
//设置对应状态的按钮的图片
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
//设置对应状态的按钮背景图片
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
UIButton的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)
UIControlStateApplication = 0x00FF0000, // additional flags available for application use
UIControlStateReserved = 0xFF000000 // flags reserved for internal framework use
};