点击按钮改变颜色和状态之二

之前写过一个关于按钮改变状态的片段,但是有点繁琐,最近get了一个简单的方法。

1.首先设置一个全局变量_startBtn,用来转换选中时的button

@interface OrderViewController ()<UITableViewDataSource, UITableViewDelegate>{

    UIButton *_stateBtn;//

    UIButton *_startBtn;

}

2.布局

#pragma mark -- 布局订单按钮的状态

-(void)LayoutOrderState {

    UIView *stateView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, UIScreenWidth, 40)];

    stateView.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:stateView];

    for (int i = 0; i < 4; i++) {

        _stateBtn = [UIButton buttonWithType:UIButtonTypeSystem];

        _stateBtn.frame = CGRectMake(0 + (UIScreenWidth - 3) / 4 * i, 64, (UIScreenWidth - 3) / 4, 40);

        NSArray *stateArr = @[@"全部", @"待付款", @"配送中", @"待收货"];

        [_stateBtn setTitle:stateArr[i] forState:UIControlStateNormal];

        _stateBtn.titleLabel.font = [UIFont systemFontOfSize:14];

        _stateBtn.tag = 100 + i;

        //点击事件

        [_stateBtn addTarget:self action:@selector(handleAction:) forControlEvents:UIControlEventTouchUpInside];

        if (100 == _stateBtn.tag) {

            [_stateBtn setTitleColor:[UIColor colorWithRed:250 / 255.0 green:60 / 255.0 blue:6 / 255.0 alpha:1] forState:UIControlStateNormal];

            _startBtn = _stateBtn;//此时将第一个按钮赋给转换按钮

        }else {

            [_stateBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        }

        [self.view addSubview:_stateBtn];

       

    }

   

}

3.点击事件

//状态按钮的点击事件

-(void)handleAction:(UIButton *)button {

    [_startBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [button setTitleColor:[UIColor colorWithRed:250 / 255.0 green:60 / 255.0 blue:6 / 255.0 alpha:1] forState:UIControlStateNormal ];

    _startBtn = button;

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值