UILabel sizeToFit and constraints

前端 未结 3 1463
终归单人心
终归单人心 2020-12-13 01:28

Is there any simple way which can help me to change position of dependent views dynamically using their content size?

I want to show several views in column which al

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-13 01:35

    This works too. https://github.com/jszumski/auto-layout-table-cells/blob/master/DynamicCellHeights/JSLabel.m

    @interface JSLabel : UILabel
    
    @end
    
    @implementation JSLabel
    
    - (id)init {
        self = [super init];
    
        // required to prevent Auto Layout from compressing the label (by 1 point usually) for certain constraint solutions
        [self setContentCompressionResistancePriority:UILayoutPriorityRequired
                                              forAxis:UILayoutConstraintAxisVertical];
    
        return self;
    }
    
    - (void)layoutSubviews {
        [super layoutSubviews];
    
        self.preferredMaxLayoutWidth = CGRectGetWidth(self.bounds);
    
        [super layoutSubviews];
    }
    
    @end
    

提交回复
热议问题