美文网首页
Button的Disable状态不显示

Button的Disable状态不显示

作者: follow_er | 来源:发表于2021-10-29 18:18 被阅读0次

    问题: 一个按钮设置了3种状态,normal, selected, disable 状态, 我先设置了button的选中,又设置了button的不选中,又设置了button的选中,最后设置了button的禁用状态,最后的禁用状态没有显示,显示的UI仍然是normal状态的,不能正常显示disable的样式。

    解决方法: 要让选中状态与禁用状态合并到一起

    OC版本

    UIControlState是位移枚举,OC可以使用”|“和”&“,这两个是C语言的运算符

    
    @interface ViewController ()
    //记录次数测试不同状态
    @property (nonatomic, assign) NSInteger clickCount;
    @end
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    //设置一个测试的button
    UIButton *btnRZ = [[UIButton alloc] init];
        btnRZ.selected = true;
        [btnRZ setTitle:@"正常" forState:UIControlStateNormal];
        [btnRZ setTitle:@"选中" forState:UIControlStateSelected];
    // 注意这里禁用 |  选中
        [btnRZ setTitle:@"禁止" forState:UIControlStateDisabled | UIControlStateSelected];
        [btnRZ setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        [btnRZ setTitleColor:[UIColor yellowColor] forState:UIControlStateSelected];
    // 注意这里禁用 |  选中
        [btnRZ setTitleColor:[UIColor redColor] forState:UIControlStateDisabled | UIControlStateSelected];
        [btnRZ addTarget:self action:@selector(onClickBtnRZ:) forControlEvents:UIControlEventTouchUpInside];
        btnRZ.backgroundColor = [UIColor blackColor];
        [self.view addSubview: btnRZ];
        btnRZ.frame = CGRectMake(45, 200, 300, 50);
    }
    // button 调用的方法
    - (void)onClickBtnRZ:(UIButton *)sender {
        _clickCount += 1;
        if (_clickCount % 3 == 1) {
            sender.selected = NO;
        } else if (_clickCount % 3 == 2) {
            sender.selected = YES;
        } else if (_clickCount % 3  == 0) {
            sender.enabled = NO;
        }
    }
    

    Swift版本

    Swift不能和C语言混编,所以在swift中已经没有”|“和”&“这样的运算了 ,需要使用的是OptionSet协议

    var clickCount = 0
    
    override func viewDidLoad() {
            super.viewDidLoad()
            
            let btnRZ = UIButton()
            btnRZ.isSelected = true
            btnRZ.setTitle("正常", for: .normal)
            btnRZ.setTitle("选中", for: .selected)
    // 注意这里[禁用 ,  选中]
            btnRZ.setTitle("禁止", for: [.disabled, .selected])
            btnRZ.setTitleColor(UIColor.green, for: .normal)
            btnRZ.setTitleColor(UIColor.yellow, for: .selected)
    // 注意这里[禁用 ,  选中]
            btnRZ.setTitleColor(UIColor.red, for: [.disabled, .selected])
            btnRZ.addTarget(self, action: #selector(onClickBtnRZ(sender:)), for: .touchUpInside)
            btnRZ.backgroundColor = UIColor.black
            self.view.addSubview(btnRZ)
            btnRZ.frame = CGRect.init(x: 45, y: 200, width: 300, height: 50)
    }
    
        @objc func onClickBtnRZ(sender: UIButton){
            clickCount += 1
            if clickCount % 3 == 1 {
                sender.isSelected = false
            } else if clickCount % 3 == 2 {
                sender.isSelected = true
            } else if clickCount % 3  == 0 {
                sender.isEnabled = false
            }
        }
    
    2021-10-29_2281841392291411366.13.10.gif

    相关文章

      网友评论

          本文标题:Button的Disable状态不显示

          本文链接:https://www.haomeiwen.com/subject/pkzialtx.html