SDAutoLayout的学习使用(一)

一、 XXXSapceToView(view, space)

这里XXX代表top,left, right,bottom。该方法传入两个参数,代表距离这个View的距离多少

二、XXXEqualToView(view)

这里XXX代表top,left, right,bottom。该方法传入一个参数,代表与这个View的top,left, right,bottom对齐

三、XXXIs

这里XXX指定View的属性如x,y,width,height等,代表给该属性指定一个值

四、autoHeightRatio

高与宽的比例,label的话可以直接传入0代表高度自适应。

五、高度宽度自适应

/** 设置Cell的高度自适应,也可用于设置普通view内容高度自适应 */

- (void)setupAutoHeightWithBottomView:(UIView *)bottomView bottomMargin:(CGFloat)bottomMargin;

/** 用于设置普通view内容宽度自适应 */

- (void)setupAutoWidthWithRightView:(UIView *)rightView rightMargin:(CGFloat)rightMargin;

其中高度自适应是最常用到的

代码如下

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setup];
    }
    return self;
}
- (void)setup
{
    UILabel *titleLabel = [UILabel new];
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    UILabel *contentLabel = [UILabel new];
    contentLabel.textColor = [UIColor lightGrayColor];
    contentLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:contentLabel];
    self.contentLabel = contentLabel;
    
    CGFloat margin = 10;
    
    self.titleLabel.sd_layout
//    左
    .leftSpaceToView(self.contentView, margin)
//    上
    .topSpaceToView(self.contentView, margin)
//    右
    .rightSpaceToView(self.contentView, margin)
//    自身高度
    .heightIs(20);
    
    self.contentLabel.sd_layout
//    与self.titleLabel 左右对齐
    .leftEqualToView(self.titleLabel)
    .rightEqualToView(self.titleLabel)
//    与self.titleLabel 上 距10
    .topSpaceToView(self.titleLabel, margin)
//    文字自适应高度
    .autoHeightRatio(0);
    
    [self setupAutoHeightWithBottomView:self.contentLabel bottomMargin:margin];
}

你可能感兴趣的:(SDAutoLayout的学习使用(一))