美文网首页
2017-12-27

2017-12-27

作者: 曼纪珂 | 来源:发表于2017-12-27 10:38 被阅读0次

    IOS轻松搞定button单选,多选,反选标签

    (1)创建KKTipView继承于UIView

    .h文件内容

    //#import@interface KKTipsView : UIView

    @property(nonatomic,strong)UIButton        * button;

    @property(nonatomic,strong)NSMutableArray  * AddData;

    @property(nonatomic,copy)void(^selectBlock)(NSInteger flag);

    @end

    .m文件内容

    #import "KKTipsView.h"

    #import "UIColor+Factory.h"

    @implementation KKTipsView

    -(instancetype)initWithFrame:(CGRect)frame{

        self=[super initWithFrame:frame];

        if (self) {

            [self createUI];

        }

        return self;

    }

    -(void)createUI{

        for (int i = 0; i < 12; i++)

        {

            int a = i/6;

            int b = i%5;

            self.button = [[UIButton alloc] initWithFrame:CGRectMake(10+(10+(self.frame.size.width-6)/6)*b, 15+(30+10)*a, (self.frame.size.width-30)/6, 30)];

            self.button.layer.borderColor = [UIColor mainGrayColor].CGColor;

            self.button.layer.borderWidth = 1;

            self.button.layer.cornerRadius = 5;

            self.button.tag = i;

            self.button.titleLabel.font = [UIFont systemFontOfSize:12];

            [self.button setTitle:@"标签" forState:UIControlStateNormal];

            [self.button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

            [self.button setBackgroundColor:[UIColor mainGrayColor]];

            [self.button addTarget:self action:@selector(tagButtonAction:) forControlEvents:UIControlEventTouchUpInside];

            [self addSubview:self.button];

        }

    }

    /**

    * 单选

    */

    -(void)tagButtonAction:(UIButton *)bt{

        if (bt.selected == YES) {

            [bt setBackgroundColor:[UIColor mainGrayColor]];

            [bt setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

            bt.selected = NO;

        }else{

            [bt setBackgroundColor:UIColorFromRGB(0x2bdcff)];

            [bt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

            bt.selected = YES;

        }

            if (_selectBlock) {

                _selectBlock(1);

            }

    }

    谢谢支持!!!

    相关文章

      网友评论

          本文标题:2017-12-27

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