带有砌体的AutoLayout会收到警告

时间:2015-01-16 15:21:06

标签: ios autolayout

我在顶部添加一个按钮,在tableviewcell中使用自动布局下面的标签下方的标签,使用masnory,一切都运行良好,但我得到一些消息,如警告。

行高计算

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.mycell == nil) {
        self.mycell = [[AutoLayoutTableViewCell alloc] init];
    }
    self.mycell.label.text = [self.data objectAtIndex:indexPath.row];
    [self.mycell setNeedsUpdateConstraints];
    [self.mycell updateConstraintsIfNeeded];
    self.mycell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(self.mycell.bounds));
    [self.mycell setNeedsLayout];
    [self.mycell layoutIfNeeded];
    CGFloat height = [self.mycell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    height += 1;
    return height;
}

这是我的约束

    UIView *root = self.contentView;

    [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(80);
        make.top.equalTo(root).offset(16);
        make.leading.top.equalTo(root).offset(16);
        make.trailing.equalTo(root).offset(-16);
    }];

    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.btn.mas_bottom).offset(10);
        make.leading.equalTo(root).offset(16);
        make.trailing.equalTo(root).offset(-16);
        make.bottom.equalTo(root).offset(-16);
    }];

并收到警告信息。

Probably at least one of the constraints in the following list is one you don't want. Try this: 
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 

(
"<NSLayoutConstraint:0x7fdce2437210 UITableViewCellContentView:0x7fdce242a900.height == 44>",
"<MASLayoutConstraint:0x7fdce243aa40 UIButton:0x7fdce2434620.height == 80>",
"<MASLayoutConstraint:0x7fdce243b080 UIButton:0x7fdce2434620.top == UITableViewCellContentView:0x7fdce242a900.top + 16>",
"<MASLayoutConstraint:0x7fdce243bcc0 UILabel:0x7fdce2427cc0.top == UIButton:0x7fdce2434620.bottom + 10>",
"<MASLayoutConstraint:0x7fdce243c550 UILabel:0x7fdce2427cc0.bottom == UITableViewCellContentView:0x7fdce242a900.bottom - 16>"
)

Will attempt to recover by breaking constraint 
<MASLayoutConstraint:0x7fdce243aa40 UIButton:0x7fdce2434620.height == 80>

1 个答案:

答案 0 :(得分:0)

你的单元格高44磅(参见错误信息中的第一个约束条件),但你在单元格内垂直固定标签和按钮,它们的组合高度高于44磅。你可能希望你的细胞更高。您可以通过在rowHeight上设置UITableView或使用iOS 8’s self-sizing cells来完成此操作:

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension