ios - 如何用 masonry 根据传进来的个数动态的布局子视图

 ZQ我是疯癫小karmenRJ 发布于 2022-10-29 01:11

我现在想自定义一个控件, 该控件初始化的时候会传入一个数组,我需要根据数组的个数创建若干个UIButton

button的宽度是根据控件的宽度和各button的间距算出来的, button之间等间距..

请问这种情况如何用masonry来布局这些button? (这个控件也需要用Masonry进行约束, 所以不会给它的frame赋值)

我将我使用masonry前后的代码贴了出来, 关键点heightwidth我用问好代替了,希望大神们能够赐教!

不使用masonry是这样布局 :

- (instancetype)initWithFrame:(CGRect)frame items:(NSArray *)items
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = YZ_WhiteColor;
        
        NSInteger count = items.count;
        
        for (int i = 0; i < count; i++) {
            
            NSInteger width  = (self.bounds.size.width - margin * 2 - (count - 1) * gap) / count;
            NSInteger height = btnHeight;
            NSInteger x = margin + i * (gap + width);
            NSInteger y = 10;
            
            UIButton *btn = [self buttonForToolBarWithTitle:items[i]];
            btn.frame = CGRectMake(x, y, width, height);
            btn.tag = 100 + i;
            btn.layer.cornerRadius = 5;
        }
    }
    return self;
}

使用masonry布局 :

- (instancetype)initWithFrame:(CGRect)frame items:(NSArray *)items
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = YZ_WhiteColor;
        
        NSInteger count = items.count;
        
        for (int i = 0; i < count; i++) {
            
            UIButton *btn = [self buttonForToolBarWithTitle:items[i]];
            btn.tag = 100 + i;
            btn.layer.cornerRadius = 5;
            
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self);
                make.height.mas_equalTo(btnHeight);
                make.width.equalTo(???);
                make.left.equalTo(???);
            }];
        }
    }
    return self;
}
3 个回答
  • 将所有button的width都设为相等不就好了

    2022-10-30 05:24 回答
  • 大概是这样:
    - (instancetype)initWithFrame:(CGRect)frame items:(NSArray<NSString *> *)items
    {
        if (self = [super initWithFrame:frame]) {
            self.backgroundColor = YZ_WhiteColor;
            NSInteger count = items.count;
            
            UIButton *lastButton = nil;
            for (int i = 0; i < count; i++) {
                UIButton *btn = [self buttonForToolBarWithTitle:items[i]];
                btn.tag = 100 + i;
                btn.layer.cornerRadius = 5;
                NSInteger width  = (self.bounds.size.width - margin * 2 - (count - 1) * gap) / count;
                [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.equalTo(self);
                    make.height.mas_equalTo(btnHeight);
                    make.width.equalTo(width);
                    
                    if (lastButton) {
                        make.left.equalTo(lastButton.mas_right).offset(margin);
                    }else{
                        make.left.equalTo(self).offset(margin);
                    }
                }];
                lastButton = btn;
            }
        }
        return self;
    }
    2022-10-30 05:24 回答
  • 我参考Masonry给出的Demo得到了答案

    - (instancetype)initWithItems:(NSArray <NSString *>*)items
    {
        if (self = [super initWithFrame:CGRectZero]) {
            
            self.backgroundColor = YZ_WhiteColor;
            
            NSInteger count = items.count;
            NSMutableArray *arr = @[].mutableCopy;
            
            for (int i = 0; i < count; i++) {
                
                UIButton *btn = [self buttonForToolBarWithTitle:items[i]];
                btn.tag = 100 + i;
                btn.layer.cornerRadius = 5;
                [arr addObject:btn];
            }
            
            if (count > 1) {
                [arr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:gap leadSpacing:margin tailSpacing:margin];
                [arr mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.equalTo(self);
                    make.height.mas_equalTo(btnHeight);
                }];
            }
            else {
                [self.subviews.firstObject mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.equalTo(self);
                    make.height.mas_equalTo(btnHeight);
                    make.left.equalTo(self).offset(margin);
                    make.right.equalTo(self).offset(-margin);
                }];
            }
        }
        return self;
    }
    
    2022-10-30 05:25 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有