UIButton
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UIButton相关的知识,希望对你有一定的参考价值。
//创建一个UIButton对象,UIButton侧重于交互,响应事件
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//设置frame、
button.frame = CGRectMake(100, 300, 200, 150);
//设置button显示的文本(标题)
//syetem默认蓝色,custom需要给颜色
[button setTitle:@"点~我~啊" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
//设置button的背景图片
// [button setBackgroundImage:[UIImage imageNamed:@"xigua.png"] forState:UIControlStateNormal];
//
// [button setBackgroundImage:[UIImage imageNamed:@"BtnOff"] forState:UIControlStateHighlighted];
//设置button的前景图片
//设置前景图片时,使用custom样式,如果图片大小大于button的大小,那么图片会被压缩到与button等大,如果小于button的大小,图片保留原有大小
//设置背景图片时,不论是custom还是syetem,图片
// 都会与button等大
[button setImage:[UIImage imageNamed:@"xigua"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"BtnOff"] forState:UIControlStateHighlighted];
//关键方法,为button添加一个事件
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
//点击时如果设置了图片,不出现闪烁的效果。
// button.adjustsImageWhenHighlighted = NO;
[containerView addSubview:button];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(10, 300, 30, 30);
[btn setBackgroundImage:[UIImage imageNamed:@"check"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"checked"] forState:UIControlStateSelected];
[btn addTarget:self action:@selector(change:) forControlEvents:UIControlEventTouchUpInside];
button.adjustsImageWhenHighlighted = NO;
[containerView addSubview:btn];
[self.window makeKeyAndVisible];
return YES;
}
- (void)click:(UIButton *)button {
NSLog(@"吃大西瓜");
//1、通过标记值102找到containerView
UIView *view = [self.window viewWithTag:102];
//2、通过标记值101找到输入框
UITextField *textField = (UITextField *)[view viewWithTag:101];
//3、放弃第一响应者
[textField resignFirstResponder];
}
- (void)change:(UIButton *)button {
button.selected = !button.selected;
}
以上是关于UIButton的主要内容,如果未能解决你的问题,请参考以下文章