无法同时满足UITableView中的约束

时间:2015-04-26 12:31:53

标签: ios uitableview nslayoutconstraint

在UiTableView中

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ID = @"cell";
LZHThreadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
    cell = [[LZHThreadTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}

[cell configureThread:self.threads[indexPath.row]];

return cell;
}

和UITableViewCell

-(id)configureThread:(LZHThread *)thread{
[_avatarImageView sd_setImageWithURL:thread.user.avatarImageURL];
_avatarImageView.translatesAutoresizingMaskIntoConstraints=NO;
_userNameLabel.text=thread.user.userName;
[_userNameLabel sizeToFit];
_userNameLabel.translatesAutoresizingMaskIntoConstraints=NO;
_countLabel.text=[NSString stringWithFormat:@"%ld/%ld",thread.replyCount,thread.totalCount];
[_countLabel sizeToFit];
_countLabel.translatesAutoresizingMaskIntoConstraints=NO;
_postTimeLabel.text=thread.postTime;
[_postTimeLabel sizeToFit];
_postTimeLabel.translatesAutoresizingMaskIntoConstraints=NO;
_titleLabel.text=thread.title;
_titleLabel.numberOfLines=0;
_titleLabel.lineBreakMode=NSLineBreakByCharWrapping;
CGSize optSize=[_titleLabel sizeThatFits:CGSizeMake(self.contentView.frame.size.width-2*kDistanceBetweenViews, 99999)];
_titleLabel.frame=CGRectMake(0, 0, optSize.width, optSize.height);
_titleLabel.translatesAutoresizingMaskIntoConstraints=NO;
[self configureViewConstrains];
return self;
}

我在以下方法中设置了约束

-(void)configureViewConstrains{
//_avatarImageView
NSLayoutConstraint *avatarConstrainX=[NSLayoutConstraint constraintWithItem:_avatarImageView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:kDistanceBetweenViews];
NSLayoutConstraint *avatarConstrainY=[NSLayoutConstraint constraintWithItem:_avatarImageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:kDistanceBetweenViews];
NSLayoutConstraint *avatarConstrainWidth=[NSLayoutConstraint constraintWithItem:_avatarImageView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeWidth multiplier:0.0 constant:kAvatarImageViewSize];
NSLayoutConstraint *avatarConstrainHeight=[NSLayoutConstraint constraintWithItem:_avatarImageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeHeight multiplier:0.0 constant:kAvatarImageViewSize];
[self.contentView addConstraints:@[avatarConstrainX,avatarConstrainY,avatarConstrainWidth,avatarConstrainHeight]];

//_userNameLabel
NSLayoutConstraint *userNameConstrainX=[NSLayoutConstraint constraintWithItem:_userNameLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_avatarImageView attribute:NSLayoutAttributeRight multiplier:1.0 constant:kDistanceBetweenViews];
NSLayoutConstraint *userNameConstrainY=[NSLayoutConstraint constraintWithItem:_userNameLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:_avatarImageView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0];
NSLayoutConstraint *userNameConstrainWidth=[NSLayoutConstraint constraintWithItem:_userNameLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:_avatarImageView attribute:NSLayoutAttributeWidth multiplier:0.0 constant:_userNameLabel.frame.size.width];
NSLayoutConstraint *userNameConstrainHeight=[NSLayoutConstraint constraintWithItem:_userNameLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:_avatarImageView attribute:NSLayoutAttributeHeight multiplier:0.0 constant:_userNameLabel.frame.size.height];
[self.contentView addConstraints:@[userNameConstrainX,userNameConstrainY,userNameConstrainWidth,userNameConstrainHeight]];

//_postTimeLabel
NSLayoutConstraint *postTimeConstrainX=[NSLayoutConstraint constraintWithItem:_postTimeLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-kDistanceBetweenViews-_postTimeLabel.frame.size.width];
NSLayoutConstraint *postTimeConstrainY=[NSLayoutConstraint constraintWithItem:_postTimeLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:_avatarImageView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0];
NSLayoutConstraint *postTimeConstrainWidth=[NSLayoutConstraint constraintWithItem:_postTimeLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeWidth multiplier:0.0 constant:_postTimeLabel.frame.size.width];
NSLayoutConstraint *postTimeConstrainHeight=[NSLayoutConstraint constraintWithItem:_postTimeLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeHeight multiplier:0.0 constant:_postTimeLabel.frame.size.height];
[self.contentView addConstraints:@[postTimeConstrainX,postTimeConstrainY,postTimeConstrainWidth,postTimeConstrainHeight]];

//_countLabel
NSLayoutConstraint *countConstrainX=[NSLayoutConstraint constraintWithItem:_countLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_postTimeLabel attribute:NSLayoutAttributeLeft multiplier:1.0 constant:-kDistanceBetweenViews-_countLabel.frame.size.width];
NSLayoutConstraint *countConstrainY=[NSLayoutConstraint constraintWithItem:_countLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:_avatarImageView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0];
NSLayoutConstraint *countConstrainWidth=[NSLayoutConstraint constraintWithItem:_countLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeWidth multiplier:0.0 constant:_countLabel.frame.size.width];
NSLayoutConstraint *countConstrainHeight=[NSLayoutConstraint constraintWithItem:_countLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeHeight multiplier:0.0 constant:_countLabel.frame.size.height];
[self.contentView addConstraints:@[countConstrainX,countConstrainY,countConstrainWidth,countConstrainHeight]];

//_titleLabel
NSLayoutConstraint *titleConstrainX=[NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_avatarImageView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0];
NSLayoutConstraint *titleConstrainY=[NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_avatarImageView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:kDistanceBetweenViews];
NSLayoutConstraint *titleConstrainWidth=[NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeWidth multiplier:0.0 constant:_titleLabel.frame.size.width];
NSLayoutConstraint *titleConstrainHeight=[NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeHeight multiplier:0.0 constant:_titleLabel.frame.size.height];
NSLayoutConstraint *titleConstrainBottom=[NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-kDistanceBetweenViews];
[self.contentView addConstraints:@[titleConstrainX,titleConstrainY,titleConstrainWidth,titleConstrainHeight,titleConstrainBottom]];
 }

然后在uitableview我得到高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
LZHThreadTableViewCell *cell=[[LZHThreadTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
[cell configureThread:self.threads[indexPath.row]];
CGSize optSize=[cell.contentView systemLayoutSizeFittingSize: UILayoutFittingCompressedSize];
return optSize.height;
}

但是有警告发生。

"<NSLayoutConstraint:0x7ff7a96cfd90 V:|-(8)-[UIImageView:0x7ff7a96a9390]   (Names: '|':UITableViewCellContentView:0x7ff7a96b6e00 )>",
"<NSLayoutConstraint:0x7ff7a96c1260 V:[UIImageView:0x7ff7a96a9390(34)]>",
"<NSLayoutConstraint:0x7ff7a96dcb20 V:[UIImageView:0x7ff7a96a9390]-(8)-[UILabel:0x7ff7a96afa10'0\Ud83d\Udcce']>",
"<NSLayoutConstraint:0x7ff7a96dcbf0 V:[UILabel:0x7ff7a96afa10'0\Ud83d\Udcce'(20.5)]>",
"<NSLayoutConstraint:0x7ff7a96c3870 UILabel:0x7ff7a96afa10'0\Ud83d\Udcce'.bottom == UITableViewCellContentView:0x7ff7a96b6e00.bottom - 8>",
"<NSLayoutConstraint:0x7ff7a958e4c0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7ff7a96b6e00(78)]>"

将尝试通过违反约束来恢复

我不知道发生了什么事?

1 个答案:

答案 0 :(得分:0)

看起来它说你给出的身高限制是矛盾的。从我上面看到的,你有:

  1. 一个8像素的空间,
  2. 然后是34像素的图像视图,
  3. 然后是另外8个像素空间,然后是一个20.5像素高且
  4. 的标签
  5. 你说这个标签的底部距离单元格内容视图底部8个像素。
  6. 让我们添加这些数字:

    8 + 34 + 8 + 20.5 = 70.5 像素高

    但看起来你特别说你的标签底部需要距离UITableViewCell底部8像素,即78像素。

    如果从78像素减去8像素,那么你会得到70像素高,这与我们刚加入的70.5像素相冲突。

    您可能希望再次检查约束:D