iOS开发 ☞ UIButton详解

一、自定义按钮
1、点击按钮时消除变暗状态

UIButton *customBtn = [UIButton buttonWithType:UIButtonTypeCustom];

如果只设置了普通状态下的按钮图片那么点击时(高亮状态下)图片会变暗

[customBtn setBackgroundImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];

如下设置即可消除变暗效果

 customBtn.adjustsImageWhenHighlighted = NO;

2、属性比较

@property(nonatomic,getter=isSelected) BOOL selected; 
@property(nonatomic,getter=isEnabled) BOOL enabled;                                  // default is YES. if NO, ignores touch events and subclasses may draw differently

如果设置选择状态为NO,按钮可以接收点击事件,但是如果设置按钮的enable属性为NO,按钮不能接收点击事件
enable属性的效果如下:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = [UIColor orangeColor];
    [btn setBackgroundImage:[UIImage imageNamed:@"2"] forState:UIControlStateNormal];
    btn.selected = NO;
    btn.frame = CGRectMake(100, 100, 40, 40);
    [self.view addSubview:btn];

}
- (void)btnClick:(UIButton *)btn {

    btn.enabled = !btn.enabled;
}

这里写图片描述
点击后:
这里写图片描述

你可能感兴趣的:(ios开发,UIButton)