简体   繁体   中英

Unable to simultaneously satisfy constraints in UITableView

In 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;
}

and in 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;
}

I set up constrains in the following method

-(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]];
 }

and then in uitableview i get the height

-(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;
}

But there is warning occurs.

"<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)]>"

)

Will attempt to recover by breaking constraint

I don't know what is going on?

Looks like it's saying the height constraints you have given is conflicting. From what I can see above, you have:

  1. a 8 pixel space,
  2. then a 34 pixel image view,
  3. then another 8 pixel space then a label that's 20.5 pixels tall and
  4. you said the bottom of that label is 8 pixels away from the bottom of the cell's content view.

Let's add those numbers up:

8 + 34 + 8 + 20.5 = 70.5 pixels tall

But it looks like you specially say the bottom of your label needs to be 8 pixels away from the bottom of the UITableViewCell which is 78 pixels.

If you minus 8 pixels from 78 pixels that would give you 70 pixels tall, which conflicts with the 70.5 pixels that we just added up.

You might want to check your constraints again :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM