UIButton有自己的初始化方法:
[UIButton buttonWithType:<#(UIButtonType)#>];
注:UIButton在MRC(MannulReference Counting手动管理内存)下不需要进行release。
UIButtonType是个枚举类型,其中UIButtonTypeDetailDisclosure,UIButtonTypeInfoDark,UIButtonTypeInfoLight是中间有个i,如图:
UIButtonTypeContactAdd是中间有个加号:
其他的方法UIButtonTypeSystem,UIButtonTypeRoundedRect,以及UIButtonTypeCustom则是创建的普通button,并且UIButtonTypeRoundedRect = UIButtonTypeSystem。
UIButon的用户交互默认是开启的,并且有点击方法:
target:执行点击事件的对象
action: 方法选择器
forControlEvents:按钮的点击状态
addTarget:<#(nullable id)#> action:<#(nonnull SEL)#> forControlEvents:<#(UIControlEvents)#>
外观控制:
UIButton标题的默认颜色为白色
设置指定状态下的标题
- (void)setTitle:(NSString *)title forState:(UIControlState)state
设置指定状态下的标题
- (NSString *)titleForState:(UIControlState)state
设置指定状态下标题的颜色
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state
设置指定状态下标题的颜色
- (UIColor *)titleColorForState:(UIControlState)state
设置指定状态下的标题阴影颜色
- (UIColor *)titleShadowColorForState:(UIControlState)state
设置指定状态下的标题阴影颜色
- (UIColor *)titleShadowColorForState:(UIControlState)state
// 给按钮设置背景图片
#pragma mark 设置按钮的前景图片
- (void)setImage:(UIImage *)image forState:(UIControlState)state
// [btn setImage:[UIImage imageNamed:@"check"] forState:UIControlStateNormal];
#pragma mark 设置背景图片 能自动拉伸
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
[btn setBackgroundImage:[UIImage imageNamed:@"check"] forState:UIControlStateNormal];
------------------------------------------------
// 设置按钮圆角,layer用于渲染
btn.layer.cornerRadius = 7;
btn.layer.masksToBounds = YES;
//设置阴影:
button.layer.shadowOffset = CGSizeMake(2, 3);
button.layer.shadowOpacity = 0.4;
button.layer.shadowColor = [UIColor blackColor].CGColor;