UIButton总结
1、初始化
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(100, 100, 60, 60)];
[self.view addSubview:btn];
2、常用设置
设置文字、文字颜色、图片
[btn setTitle:@"盖" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
[optionButton setTitleEdgeInsets:UIEdgeInsetsMake(2, 2, 2, 2)];//文字内边距
[btn1 setImage:[UIImage imageNamed:@"beer-cap"] forState:UIControlStateNormal];//image会覆盖文字
[self.imageButton setAdjustsImageWhenDisabled:NO];//自适应图片的大小
设置背景图、背景颜色
[btn1 setBackgroundImage:[UIImage imageNamed:@"beer-cap"] forState:UIControlStateNormal];
[btn1 setBackgroundColor:[UIColor blueColor]];//背景图部分透明时配合使用
选中
[self.cuttentOptionButton setSelected:NO];
设置tag
[btn1 setTag:21];
UIButton * btn2 = [self.view viewWithTag:21];//根据tag获取
隐藏、可用
button.hidden = YES;或 [button isHidden];
button.enabled = YES;//可用
获取title
[button titleForState:UIControlStateNormal];//根据状态获取
几个常用的只读属性,用于判断、赋值
NSString *currentTitle; // normal/highlighted/selected/disabled. can return nil
UIColor *currentTitleColor; // normal/highlighted/selected/disabled. always returns non-nil. default is white(1,1)
UIColor *currentTitleShadowColor; // normal/highlighted/selected/disabled.
UIImage *currentImage; // normal/highlighted/selected/disabled. can return nil
UIImage *currentBackgroundImage; // normal/highlighted/selected/disabled. can return nil
NSAttributedString *currentAttributedTitle NS_AVAILABLE_IOS(6_0); // normal/highlighted/selected/disabled. can return nil
3、添加事件
[button addTarget:self action:@selector(btnMove:) forControlEvents:UIControlEventTouchUpInside];//带参,参数为按钮本身
4、按钮组的遍历
[self.answerMap enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if(obj...){
...
*stop = YES;
}
}];//遍历中根据每个元素的情况操作
[view.subviews makeObjectPerformSelector:@selector(removeFromSuperview)];//对所有元素使用同一个方法
5、由UIButton推及,UIView常用的属性、方法
属性:
_optionView.userInteractorEnabled = NO;//禁止该区的用户交互
方法: