Search code examples
iosobjective-cautolayout

iOS Autolayout height is ambiguous for UILabel


ambiguous warning

Height is ambiguous for UILabel (topCommentLabel)
Height and vertical position is ambiguous for UIView (commentContainerView)

In the tableview cell using

_tableView.rowHeight = UITableViewAutomaticDimension;

I have one containerView with 3 Labels in it and I want the Label's content hold up the containerView

here is my constriants

{// comment area
        [self.commentContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self.contentLabel);
            make.top.mas_equalTo(self.contentLabel.mas_bottom).mas_offset(10);
        }];
        
        [self.topCommentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.mas_equalTo(0);
        }];
        
        [self.bottomCommentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.topCommentLabel.mas_bottom).mas_offset(0);
            make.left.right.mas_equalTo(0);
        }];
        
        [self.readAllCommentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.bottomCommentLabel.mas_bottom).mas_offset(0);
            make.left.bottom.mas_equalTo(0);
        }];
    }

the view displays normal but I got the ambiguous warning

Why this warning happen and How can I fix it?


Solution

  • You're missing vertical hugging priority. If you super view's height constraint with its content, it will need at least 1 element, in this example is a Label with higher (252) or lower (250) priority with others. Setting hugging priority to one of your label to ignore this layout warning.