UIButton的使用
2015-04-23 08:44
603 查看
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_window = [[UIWindow
alloc] initWithFrame:[UIScreen
mainScreen].bounds];
_window.backgroundColor = [UIColor
whiteColor];
[_window
makeKeyAndVisible];
/*----------------------UIButton的使用------------------------*/
// //创建按钮
//如果用这种方法,无法设置类型
// UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 50, 100, 50)];
// //设置按钮的类型
// button.buttonType = UIButtonTypeRoundedRect;
UIButton *button = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
//设置显示的地方和大小
button.frame =
CGRectMake(100,
50, 100, 50);
//设置背景颜色
// button.backgroundColor = [UIColor redColor];
//设置标题,设置标题一定要绑定状态
//设置普通状态显示的标题
[button setTitle:@"按钮"
forState:UIControlStateNormal];
//设置点击(高亮)状态显示的标题
[button setTitle:@"点我了"
forState:UIControlStateHighlighted];
//设置标题的颜色
[button setTitleColor:[UIColor
redColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor
greenColor] forState:UIControlStateHighlighted];
//按钮标题字体
button.titleLabel.font = [UIFont
systemFontOfSize:15];
//文本位置<
e758
/span>
button.titleEdgeInsets =
UIEdgeInsetsMake(0,
0, 0, 0);
//偏移量
button.imageEdgeInsets =
UIEdgeInsetsMake(0,
0, 0, 0);
//设置按钮的圆角属性
button.layer.cornerRadius =
5;
//添加按钮的点击事件
//UIControlEventTouchDown:按钮点击下的那一个事件就被触发了
//UIControlEventTouchDownRepeat按钮被多次点下的时候执行事件
//UIControlEventTouchDragInside
按钮被按着拖拽的时候被执行
//UIControlEventTouchUpInside
按钮被点击后松开 执行事件,常用的
[button addTarget:self
action:@selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
//设置button的tag值
button.tag = 1;
//添加到视图上显示
[_window addSubview:button];
//创建一个系统样式的按钮
UIButton *button1 = [UIButton
buttonWithType:UIButtonTypeContactAdd];
button1.frame =
CGRectMake(100,
100, 50, 50);
button1.tag =
2;
//添加一个点击事件
[button1 addTarget:self
action:@selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
[_window addSubview:button1];
/*-----------------------自定义按钮的使用-----------------------*/
//创建一个按钮
UIButton *customButton = [UIButton
buttonWithType:UIButtonTypeCustom];
//设置frame
customButton.frame =
CGRectMake(220,
200, 80, 40);
//创建两个图片
UIImage *image1 = [UIImage
imageNamed:@"back_on_black.png"];
UIImage *image2 = [UIImage
imageNamed:@"back_on.png"];
//设置button显示的图片,则按钮的标题不会出现了
//http://blog.csdn.net/u012762130/article/details/42591983
// [customButton setImage:image1 forState:UIControlStateNormal];
// [customButton setImage:image2 forState:UIControlStateHighlighted];
//禁用按钮,此时按钮的图片颜色会变浅
// button.enabled = NO;
//如果想显示图片还显示标题,则用一下方法
[customButton setBackgroundImage:image1
forState:UIControlStateNormal];
[customButton setBackgroundImage:image2
forState:UIControlStateHighlighted];
//设置标题
[customButton setTitle:@"按钮"
forState:UIControlStateNormal];
[customButton setTitle:@"摸我了"
forState:UIControlStateHighlighted];
button.userInteractionEnabled =
NO;//交互事件
//设置点击事件
[customButton addTarget:self
action:@selector(customAction)
forControlEvents:UIControlEventTouchUpInside];
[_window addSubview:customButton];
return
YES;
}
- (void)customAction {
NSLog(@"点击了");
}
//按钮的点击事件
- (void)buttonAction:(UIButton *)button {
if (button.tag ==
1) {
NSLog(@"button被点了");
}else if (button.tag ==
2) {
NSLog(@"button1被点了");
}
}
_window = [[UIWindow
alloc] initWithFrame:[UIScreen
mainScreen].bounds];
_window.backgroundColor = [UIColor
whiteColor];
[_window
makeKeyAndVisible];
/*----------------------UIButton的使用------------------------*/
// //创建按钮
//如果用这种方法,无法设置类型
// UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 50, 100, 50)];
// //设置按钮的类型
// button.buttonType = UIButtonTypeRoundedRect;
UIButton *button = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
//设置显示的地方和大小
button.frame =
CGRectMake(100,
50, 100, 50);
//设置背景颜色
// button.backgroundColor = [UIColor redColor];
//设置标题,设置标题一定要绑定状态
//设置普通状态显示的标题
[button setTitle:@"按钮"
forState:UIControlStateNormal];
//设置点击(高亮)状态显示的标题
[button setTitle:@"点我了"
forState:UIControlStateHighlighted];
//设置标题的颜色
[button setTitleColor:[UIColor
redColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor
greenColor] forState:UIControlStateHighlighted];
//按钮标题字体
button.titleLabel.font = [UIFont
systemFontOfSize:15];
//文本位置<
e758
/span>
button.titleEdgeInsets =
UIEdgeInsetsMake(0,
0, 0, 0);
//偏移量
button.imageEdgeInsets =
UIEdgeInsetsMake(0,
0, 0, 0);
//设置按钮的圆角属性
button.layer.cornerRadius =
5;
//添加按钮的点击事件
//UIControlEventTouchDown:按钮点击下的那一个事件就被触发了
//UIControlEventTouchDownRepeat按钮被多次点下的时候执行事件
//UIControlEventTouchDragInside
按钮被按着拖拽的时候被执行
//UIControlEventTouchUpInside
按钮被点击后松开 执行事件,常用的
[button addTarget:self
action:@selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
//设置button的tag值
button.tag = 1;
//添加到视图上显示
[_window addSubview:button];
//创建一个系统样式的按钮
UIButton *button1 = [UIButton
buttonWithType:UIButtonTypeContactAdd];
button1.frame =
CGRectMake(100,
100, 50, 50);
button1.tag =
2;
//添加一个点击事件
[button1 addTarget:self
action:@selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
[_window addSubview:button1];
/*-----------------------自定义按钮的使用-----------------------*/
//创建一个按钮
UIButton *customButton = [UIButton
buttonWithType:UIButtonTypeCustom];
//设置frame
customButton.frame =
CGRectMake(220,
200, 80, 40);
//创建两个图片
UIImage *image1 = [UIImage
imageNamed:@"back_on_black.png"];
UIImage *image2 = [UIImage
imageNamed:@"back_on.png"];
//设置button显示的图片,则按钮的标题不会出现了
//http://blog.csdn.net/u012762130/article/details/42591983
// [customButton setImage:image1 forState:UIControlStateNormal];
// [customButton setImage:image2 forState:UIControlStateHighlighted];
//禁用按钮,此时按钮的图片颜色会变浅
// button.enabled = NO;
//如果想显示图片还显示标题,则用一下方法
[customButton setBackgroundImage:image1
forState:UIControlStateNormal];
[customButton setBackgroundImage:image2
forState:UIControlStateHighlighted];
//设置标题
[customButton setTitle:@"按钮"
forState:UIControlStateNormal];
[customButton setTitle:@"摸我了"
forState:UIControlStateHighlighted];
button.userInteractionEnabled =
NO;//交互事件
//设置点击事件
[customButton addTarget:self
action:@selector(customAction)
forControlEvents:UIControlEventTouchUpInside];
[_window addSubview:customButton];
return
YES;
}
- (void)customAction {
NSLog(@"点击了");
}
//按钮的点击事件
- (void)buttonAction:(UIButton *)button {
if (button.tag ==
1) {
NSLog(@"button被点了");
}else if (button.tag ==
2) {
NSLog(@"button1被点了");
}
}
相关文章推荐
- Swift UIButton的使用详解
- ios笔记UI--汤姆猫小项目(UIbutton使用)
- UIButton的使用
- (一〇九)UIButton的使用技巧 -imageView、titleLabel、圆角等
- ios学习总结(2) -- UIButton的使用
- UIButton 的使用
- iOS UILabel UIButton UIImageView使用方法
- iOS开发UI系列之UIButton的基本使用
- UIButton上使用UIEdgeInsetsMaketitle跟图片对齐
- iphone开发 UIButton使用
- UIButton使用详解
- IOS--UIButton的使用方法详细
- 【swift系列之UIButton】简单使用
- iOS--UIButton的使用方法
- UIButton基础以及使用block+UIButton处理点击事件
- swift 使用UIButton
- 使用关联对象(AssociatedObject)为UIButton添加Block响应
- UIButton的使用
- iOS开发中UIButton的使用方法总结
- ios 在使用 SDWebimage UIButton setBackgroundimage