【问题标题】:iOS Autolayout height is ambiguous for UILabeliOS Autolayout 高度对于 UILabel 不明确
【发布时间】:2021-08-05 18:59:57
【问题描述】:

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

在tableview单元格中使用

_tableView.rowHeight = UITableViewAutomaticDimension;

我有一个包含 3 个标签的 containerView,我希望 Label 的内容能够支撑 containerView

这是我的约束

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

视图显示正常,但我收到了模棱两可的警告

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

【问题讨论】:

    标签: ios objective-c autolayout


    【解决方案1】:

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

    【讨论】:

    • 当我添加 [self.topCommentLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisVertical]; to topCommentLabel 仍然收到警告你知道我该如何解决这个问题吗?
    • @HarrisHan 实际上你应该将它设置为UILayoutPriorityDefaultLow bro
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 2021-10-27
    相关资源
    最近更新 更多