AutoLayout setting UILabel height to zero

前端 未结 5 1536
暗喜
暗喜 2021-02-14 01:40

I have a UILabel for item description amongst other views, all laid out using constraints in Interface Builder - you can see all relevant constraints in the image b

5条回答
  •  说谎
    说谎 (楼主)
    2021-02-14 02:43

    I think you need to set 5 constraints on your label :

    • Leading space to superview
    • Trailing space to superview
    • Vertical space to "Item"
    • Vertical space to "Name"
    • Height

    Then add an IBOutlet in your controller on the constraint height (let's say labelHeight).

    So in your viewDidLoad you will be able to set this constraint value:

    @property (weak, nonatomic) IBOutlet UILabel *label;
    @property (weak, nonatomic) IBOutlet NSLayoutConstraint *labelHeight;
    
    
    - (void) viewDidLoad{
       [self.label sizeToFit];
       labelHeight.constant = self.label.frame.size.height;
    }
    

提交回复
热议问题