UILabel sizeToFit and constraints

前端 未结 3 1448
终归单人心
终归单人心 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:53

    If you still want to use Auto Layout Constraint for your Label. This is a solution:

    [self.lblBadgeValue sizeToFit];
    self.constraintWidthBadgeLabel.constant =  self.lblBadgeValue.frame.size.width;
    [self.lblBadgeValue needsUpdateConstraints];
    [self.lblBadgeValue layoutIfNeeded];
    
    • Explain more:
    • sizeToFit -> make label fit height, width with content of it.
    • So in runtime you need to update constraint height, or width for label
    • After that you need to say for compiler know that what need update constraint.
    • And in the end you need to call layout if have change on constraint.

提交回复
热议问题