Question

i´m using autolayout in my Custom TableViewCell which is created with a xib file, it looks like this:

enter image description here

Everything works like expected and is shown correct, but i get a lot of Warning Messages while scrolling down the TableView:

Unable to simultaneously satisfy constraints. 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:0xc3970d0 V:[UIImageView:0xc396fc0(75)]>",
"<NSLayoutConstraint:0xc3c1dc0 V:|-(10)-[UIImageView:0xc396fc0]   (Names: '|':UITableViewCellContentView:0xc396f20 )>",
"<NSLayoutConstraint:0xc3c1fc0 V:[UIImageView:0xc3b7700]-(0)-|   (Names: '|':UITableViewCellContentView:0xc396f20 )>",
"<NSLayoutConstraint:0xc3c1ff0 V:[UIImageView:0xc396fc0]-(43)-[UIImageView:0xc3b7700]>",
"<NSAutoresizingMaskLayoutConstraint:0xc3bf8c0 h=--& v=--& V:[UITableViewCellContentView:0xc396f20(126.5)]>"
)

Will attempt to recover by breaking constraint 

<NSLayoutConstraint:0xc3970d0 V:[UIImageView:0xc396fc0(75)]>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in    <UIKit/UIView.h> may also be helpful.

Especially the following Part is interesting i think:

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0xc3970d0 V:[UIImageView:0xc396fc0(75)]>

So the Problem is the Height of my UIImageview on the left Top??? I tried to delete the height and many other things, but absolutely noting worked for me....what can i do? I want a fixed size for my UIImageView on the Top Left...

EDIT: Maybe the Problem could be that the bottom UIImageView is optional and sometimes its hidden, so that leads maybe to the warnings...

The important Part of my Code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:    (NSIndexPath *)indexPath{

    static NSString *CellIdentifier1 = @"NewsCell";


    GTNewsCustomCell *newsCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];

    NSString *bottomImage = [[self.newsList objectAtIndex:indexPath.section] valueForKey:@"boardnoteImage"];

    if(bottomImage == (NSString *)[NSNull null] || bottomImage == nil || bottomImage.length == 0){

        newsCell.bottomImage.hidden = YES;
        newsCell.bottomImage.image = nil;
        newsCell.bottomConstraint.constant = 0.0f;

        [newsCell layoutIfNeeded];
    } else {

        newsCell.bottomConstraint.constant = 19.0f;
        newsCell.bottomImage.hidden = NO;
        [newsCell layoutIfNeeded];

        [newsCell.contentView addSubview:newsCell.boardNoteImage];

    }


    newsCell.titleLabel.text = [[self.newsList objectAtIndex:indexPath.section]objectForKey:@"title"];

    newsCell.messageText = [[self.newsList objectAtIndex:indexPath.section]objectForKey:@"previewMessage"];

    NSString *authorImg = [[self.newsList objectAtIndex:indexPath.section] valueForKey:@"objectImage"];


    return newsCell;
  }

EDIT 2: I made some changes and the height calculation for the cell is now:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *bottomImage = [[self.newsList objectAtIndex:indexPath.section] valueForKey:@"boardnoteImage"];

//Check if the bottom Image exists
if(bottomImage == (NSString *)[NSNull null] || bottomImage == nil || bottomImage.length == 0){
//        if it not exists the cell should be smaller
    return 128.0f;
}
//if the image exists the size should be 250
return 250;

}

EDIT: 3

FINALLY it works now. I have two solutions. First one is to set the Cell Height to 148.0f, to be honest i have no idea why, maybe some strange insets from my UITextView?). And the secnd solution is to delete the bottom Constraint of the bottom Image and set a fix Height for the Bottom Image!

Was it helpful?

Solution

Your current setup of constraints will give the bottom UIImageView a height of -1.5.

Second rule of AutoLayout - You can't have negatives!

You can see this from the error message.

From top to bottom (of the UITableViewCell) you have...

  • A gap of 10.
  • A height of 75.
  • A gap of 43.
  • A variable height (This is the big image view)
  • A gap of 0.

This adds up to 128.

Now, you also have a total height of the cell from top to bottom of 126.5.

The only way to satisfy these is to set the variable height of the image view to -1.5. THIS CAN'T BE DONE It breaks the second rule of AutoLayout. And so it chooses a constraint that it thinks might help and removes it. Thus you probably get a image view height of 0 or something.

You need to change the constraints or make the cell height 128.

OTHER TIPS

I duplicated your layout in a sample project and the only way I could reproduce your issue was by setting this in cellForRowAtIndexPath:

cell.translatesAutoresizingMaskIntoConstraints = NO;

If you're doing that, try removing it. I haven't had time to dig into the details to understand why this causes the layout to be over-constrained. But in general, I think a good rule of thumb is don't change this default value because the table view owns the layout of the cell itself. You only own the layout of the contents.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top