UILabel 的 iOS 自动布局高度不明确

时间:2021-05-16 07:18:45

标签: ios objective-c autolayout

ambiguous warning

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

在tableview单元格中使用

<块引用>

_tableView.rowHeight = UITableViewAutomaticDimension;

我有一个带有 3 个标签的容器视图,我希望标签的内容支撑容器视图

这是我的约束

{// 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);
        }];
    }

视图显示正常,但我收到了不明确的警告

为什么会出现此警告,我该如何解决?

1 个答案:

答案 0 :(得分:1)

您缺少vertical hugging priority。如果超级视图的高度约束及其内容,则至少需要 1 个元素,在此示例中是具有更高 (252) 或更低 (250) 优先级的 Label 与其他元素。将 hugging priority 设置为您的标签之一以忽略此布局警告。