• iOS 一行代码实现单行多按钮单选按钮事例


    1、经常有单行多按钮单选效果的设计,大多数人第一想到的就是for循环UI和frame来创建,但
    是项目中又引入了 masonry框架,masonry原本就是做UI布局的,那可不可以利用起来呢?答案
    是可以的;
    2、下面就使用masonry来简单实现一下单行多按钮单选效果,并且默认第一个按钮为默认按钮的场景:

    1. // @property (nonatomic, strong) UIView *rootView;
    2. // @property (nonatomic, strong) UIButton *childSelectedBtn;//声明一个全局默认按钮
    3. NSMutableArray *arraychildView = [NSMutableArray new];
    4. NSArray *titlechildArr = @[@"班级",@"院校",@"省份"];
    5. for (int i = 0; i < titlechildArr.count; i++) {
    6. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    7. button.backgroundColor = [UIColor colorWithHexString:@"F3F4F5"];
    8. button.tag = 1000 + I;
    9. [button setTitleColor:[UIColor colorWithHexString:@"000000"] forState:UIControlStateNormal];
    10. [button setTitle: titlechildArr[i] forState:UIControlStateNormal];
    11. [button addTarget:self action:@selector(childAction:) forControlEvents:UIControlEventTouchUpInside];
    12. [self.rootView addSubview:button];
    13. [arraychildView addObject:button];
    14. if(button.tag == 1000) {
    15. self.childSelectedBtn = button;
    16. button.backgroundColor = [UIColor colorWithHexString:@"292A2E"];
    17. [button setTitleColor:[UIColor colorWithHexString:@"F1E5BF"] forState:UIControlStateNormal];
    18. }
    19. }
    20. [arraychildView mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:7 leadSpacing:14 tailSpacing:14];
    21. [arraychildView mas_makeConstraints:^(MASConstraintMaker *make) {
    22. make.top.equalTo(self.titleChildLabel.mas_bottom).offset(14);
    23. }];
    24. - (void)childAction:(UIButton *)sender {
    25. // 未选中
    26. if (self.childSelectedBtn) {
    27. self.childSelectedBtn.backgroundColor = [UIColor colorWithHexString:@"F3F4F5"];
    28. [self.childSelectedBtn setTitleColor:[UIColor colorWithHexString:@"000000"] forState:UIControlStateNormal];
    29. }
    30. // 选中
    31. self.childSelectedBtn = sender;
    32. self.childSelectedBtn.backgroundColor = [UIColor colorWithHexString:@"292A2E"];
    33. [self.childSelectedBtn setTitleColor:[UIColor colorWithHexString:@"F1E5BF"] forState:UIControlStateNormal];
    34. }

     

    3、以上代码虽然功能实现了,但是如果比较多的标签场景下,代码量就很多了,需要复制一份以上代码,这样无意中增加了冗余代码,视觉上变得毫无观感;
    那还有更好的办法吗?答案是有的;可对此优化升级(包装函数),代码如下:
    引入头文件
     #import //(因为这里是动态赋值给到全局按钮)

    1. /// 单行布局按钮 封装
    2. /// - Parameters:
    3. /// - titleArr: 按钮文字集合
    4. /// - btntag: 按钮标识
    5. /// - leadSpacing: 居左距离
    6. /// - fixedSpacing: 中间间隙
    7. /// - tailSpacing: 居右距离
    8. /// - superLabel: 相对顶部label对象(这里可以根据自己的场景修改类型)
    9. /// - topEqualToViewOffset: 相对上面的UI对象的间距
    10. /// - globalButton: 将第一个按钮赋值给默认选中全局按钮 参数格式为:(selectedButton)
    11. /// - subview: 添加到的父视图对象
    12. /// - actionSEL: 按钮执行事件SEL
    13. - (void)createLoopButtonWithTitleArr:(NSArray *)titleArr
    14. buttonTag:(NSInteger)btntag
    15. leadSpacing:(CGFloat)leadSpacing
    16. fixedSpacing:(CGFloat)fixedSpacing
    17. tailSpacing:(CGFloat)tailSpacing
    18. topEqualToView:(UILabel *)superLabel
    19. topEqualToViewOffset:(CGFloat)topEqualToViewOffset
    20. globalButtonName:(NSString *)globalButton
    21. addSubview:(UIView *)subview
    22. action:(SEL)actionSEL {
    23. NSMutableArray *arrayChildView = [NSMutableArray new];
    24. int countNum = (int)titleArr.count;
    25. for (int i = 0; i < countNum; i++) {
    26. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    27. button.backgroundColor = [UIColor colorWithHexString:@"F3F4F5"];
    28. [button setTitleColor:[UIColor colorWithHexString:@"000000"] forState:UIControlStateNormal];
    29. button.titleLabel.font = [UIFont fontWithName:kFontRegular size:12];
    30. button.tag = btntag + i;
    31. [button setTitle:titleArr[i] forState:UIControlStateNormal];
    32. [button addTarget:self action:actionSEL forControlEvents:UIControlEventTouchUpInside];
    33. [subview addSubview:button];
    34. [arrayChildView addObject:button];
    35. if(button.tag == btntag) {
    36. if (globalButton && globalButton.length > 0) {
    37. globalButton = [globalButton stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[globalButton substringToIndex:1] capitalizedString]];
    38. ((void(*)(id,SEL,id))objc_msgSend)(self, NSSelectorFromString([[@"set" stringByAppendingString:globalButton] stringByAppendingString:@":"]),button);
    39. }
    40. button.backgroundColor = [UIColor colorWithHexString:@"292A2E"];
    41. [button setTitleColor:[UIColor colorWithHexString:@"F1E5BF"] forState:UIControlStateNormal];
    42. }
    43. }
    44. [arrayChildView mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:fixedSpacing leadSpacing:leadSpacing tailSpacing:tailSpacing];
    45. [arrayChildView mas_makeConstraints:^(MASConstraintMaker *make) {
    46. make.top.equalTo(superLabel.mas_bottom).offset(topEqualToViewOffset);
    47. }];
    48. }

    一行代码实现单行单选按钮事例:

        [self createLoopButtonWithTitleArr:@[@"半年内",@"一年内",@"两年内"] buttonTag:100 leadSpacing:14 fixedSpacing:7 tailSpacing:14 topEqualToView:self.titleNameLabel topEqualToViewOffset:14 globalButtonName:@"childSelectedBtn" addSubview:self.rootView action:@selector(childAction:)];
    

  • 相关阅读:
    多种隐藏滚动条但是依然可以滚动实现方式
    [ACTF2020 新生赛]Exec1
    磁盘调度算法例题解析以及C语言实现
    MATLAB中对象的保存和加载过程
    Linux基础
    Python——读取MySQL数据并导出到Excel
    K8S容忍toleration的声明语法、配置项含义及和污点的配合使用
    肖sir___银行项目讲解理财
    instanceof实现原理
    Linux(ubuntu)安装AppImage步骤
  • 原文地址:https://blog.csdn.net/chungeshihuatian/article/details/128026619