您的位置:首页 > 产品设计 > UI/UE

UIButton

2015-09-10 21:22 253 查看
UIButton 按钮控件 点击按钮,会触发某个事件

1)、初始化UIButton

UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];


//这里的样式是系统自带的一个枚举(一般我们选择的样式是UIButtonTypeCustom)

typedef NS_ENUM(NSInteger, UIButtonType) {
UIButtonTypeCustom = 0,                         // no button type
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),  // standard system button

UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,

UIButtonTypeRoundedRect = UIButtonTypeSystem,   // Deprecated, use UIButtonTypeSystem instead
};


2)、设置范围

button.frame=CGRectMake(140, 400, 100, 100);


3)、为button添加一个触发方法,使点击按钮触发一个方法

[button addTarget:self action:@selector(changeTitle:) forControlEvents:UIControlEventTouchUpInside];


4)、给button起个名字

[button setTitle:@"变变变" forState:UIControlStateNormal]


//这里forState也是系统自带的枚举

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
};


5)、设置字体颜色

[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];


6)、设置按钮点击成高亮状态

[button setTitle:@"变变变." forState:UIControlStateHighlighted];
//显示高亮

button.showsTouchWhenHighlighted=YES;


7)、为button按钮添加一个背景图片

[button setBackgroundImage:[UIImage imageNamed:@"头像1.PNG"] forState:UIControlStateNormal];


8)点击按钮变换图片(这里是高亮状态)(设置高亮状态下得图片)

[button setBackgroundImage:[UIImage imageNamed:@"头像2.png"] forState:UIControlStateHighlighted];


9)、判断button是否被点击了(按钮式否被选中)

//这里selected是一个BOOl值

button.selected=NO;


10)、设置button的背景颜色

button.backgroundColor=[UIColor whiteColor];


11)、添加button到视图上

[self.view addSubview:button];


12)、触发方法

-(void)changeTitle:(UIButton *)sender
{
//判断是否是选中状态
if(sender.selected!=YES){
}else{
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: