程序员

masonry布局使用priorityLow(),纯代码与xib

2022-01-23  本文已影响0人  马铃薯蜀黍

masonry布局,纯代码与xib结合,在自定义cell中,for循环动态创建imageView,并且自适应cell的高度。

  1. 自适应高度

放眼如今的UI界面设计,可以发现没有任何一个APP可以固定高度,包括文字的换行,图片的数量不确定的种种因素,对于自适应高度已经是基本的要求。


 - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}


  1. 优先级 priority

if (i == self.imgeArray.count -1) {
      make.bottom.equalTo(imageView.superview).offset(0).priorityLow();
  }


  1. masonry布局,纯代码与xib结合,在自定义cell中,for循环动态创建imageView


@property (weak, nonatomic) IBOutlet UIView *customImagesView;
- (void)setImgeArray:(NSArray *)imgeArray {
    _imgeArray = imgeArray;
    
    
    CGFloat height = 105;
    CGFloat margin = 14;
    CGFloat customImagesViewLeft = 107;
//        CGFloat width = 158;
    CGFloat width = (k_SCREEN_WIDTH - customImagesViewLeft - margin * 2) * 0.5;
    
    for (UIView * subView in self.customImagesView.subviews) {
        [subView removeFromSuperview];
    }
    
    for (int i = 0; i<self.imgeArray.count; i++) {
        UIImageView * imageView = [[UIImageView alloc] init];
        [imageView setContentMode:UIViewContentModeScaleAspectFill];
        [self.customImagesView addSubview:imageView];
        imageView.backgroundColor = [UIColor colorWithRandom];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            
            make.top.equalTo(imageView.superview).offset(height * (i / 2) + margin * (i / 2));
            make.left.equalTo(imageView.superview).offset(width *(i % 2) + margin * (i % 2));
            make.width.offset(width);
            make.height.offset(height);
            
            if (i == self.imgeArray.count -1) {
                make.bottom.equalTo(imageView.superview).offset(0).priorityLow();
            }
        }];
    }
}





WX20220123-141018@2x.png WX20220123-141106@2x.png WechatIMG4.jpeg
上一篇 下一篇

猜你喜欢

热点阅读