//---------------------UIButton---------------------
// 1 创建
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//*********创建圆角button
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.frame = CGRectMake(50, 50, 100, 80);
button.center = CGPointMake(180, 215);
button.tag = 100;
button1.backgroundColor = [UIColor whiteColor];
// 2 设置文字
[button1 setTitle:@"点我" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//********定义按钮标题字体格式
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
button.titleLabel.textAlignment = NSTextAlignmentCenter;
// button.titleLabel.font
// 3 添加点击事件
/**
* UIControlEventTouchDown 按钮被按下
* UIControlEventTouchDownRepeat 多次按下
* UIControlEventTouchDragInside 拖拽到按钮内部放开
* UIControlEventTouchDragOutside 拖拽到按钮外部放开
* UIControlEventTouchDragEnter 从按钮外部拖入到内部的过程
* UIControlEventTouchDragExit 从按钮内部拖入到外部的过程
* UIControlEventTouchUpInside 按钮被按下,并且在按钮内部放开
* UIControlEventTouchUpOutside 按钮被按下,并且在按钮外部放开
* UIControlEventTouchCancel 点击被取消 电话中断按钮
*
*/
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[self.view addSubview:button1];
//---------------------使用图片设置按钮---------------------
UIButton *imgButton = [UIButton buttonWithType:UIButtonTypeCustom];
imgButton.frame = CGRectMake(50, 250, 90, 44);
// 设置按钮的图片
// imageNamed: 从当前程序的资源文件夹中读取图片 png图片,可以省略扩展名,其他格式不行
UIImage *image = [UIImage imageNamed:@"back_on_black"];
// 设置普通状态下的图片
//[imgButton setImage:image forState:UIControlStateNormal];
// 设置高亮状态下的图片 当手指触摸到按钮时,按钮自动进入到高亮状态
//[imgButton setImage:[UIImage imageNamed:@"back_on"] forState:UIControlStateHighlighted];
[imgButton setBackgroundImage:image forState:UIControlStateNormal];
[imgButton setBackgroundImage:[UIImage imageNamed:@"back_on"] forState:UIControlStateHighlighted];
// 设置title
[imgButton setTitle:@"返回" forState:UIControlStateNormal];
[imgButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
// 按钮的开启和关闭
imgButton.enabled = YES;
// 开启点击效果 点击高亮
imgButton.showsTouchWhenHighlighted = YES;
// 选中
imgButton.selected = YES;
self.view.backgroundColor = [UIColor blackColor];
[self.view addSubview:imgButton];
// 1 创建
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//*********创建圆角button
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.frame = CGRectMake(50, 50, 100, 80);
button.center = CGPointMake(180, 215);
button.tag = 100;
button1.backgroundColor = [UIColor whiteColor];
// 2 设置文字
[button1 setTitle:@"点我" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//********定义按钮标题字体格式
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
button.titleLabel.textAlignment = NSTextAlignmentCenter;
// button.titleLabel.font
// 3 添加点击事件
/**
* UIControlEventTouchDown 按钮被按下
* UIControlEventTouchDownRepeat 多次按下
* UIControlEventTouchDragInside 拖拽到按钮内部放开
* UIControlEventTouchDragOutside 拖拽到按钮外部放开
* UIControlEventTouchDragEnter 从按钮外部拖入到内部的过程
* UIControlEventTouchDragExit 从按钮内部拖入到外部的过程
* UIControlEventTouchUpInside 按钮被按下,并且在按钮内部放开
* UIControlEventTouchUpOutside 按钮被按下,并且在按钮外部放开
* UIControlEventTouchCancel 点击被取消 电话中断按钮
*
*/
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[self.view addSubview:button1];
//---------------------使用图片设置按钮---------------------
UIButton *imgButton = [UIButton buttonWithType:UIButtonTypeCustom];
imgButton.frame = CGRectMake(50, 250, 90, 44);
// 设置按钮的图片
// imageNamed: 从当前程序的资源文件夹中读取图片 png图片,可以省略扩展名,其他格式不行
UIImage *image = [UIImage imageNamed:@"back_on_black"];
// 设置普通状态下的图片
//[imgButton setImage:image forState:UIControlStateNormal];
// 设置高亮状态下的图片 当手指触摸到按钮时,按钮自动进入到高亮状态
//[imgButton setImage:[UIImage imageNamed:@"back_on"] forState:UIControlStateHighlighted];
[imgButton setBackgroundImage:image forState:UIControlStateNormal];
[imgButton setBackgroundImage:[UIImage imageNamed:@"back_on"] forState:UIControlStateHighlighted];
// 设置title
[imgButton setTitle:@"返回" forState:UIControlStateNormal];
[imgButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
// 按钮的开启和关闭
imgButton.enabled = YES;
// 开启点击效果 点击高亮
imgButton.showsTouchWhenHighlighted = YES;
// 选中
imgButton.selected = YES;
self.view.backgroundColor = [UIColor blackColor];
[self.view addSubview:imgButton];