Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Autolayout and self-sizing cells in iOS8

I have setup the auto-sizing cells with UILabels with no problem in iOS8 following this tutorial: http://useyourloaf.com/blog/2014/08/07/self-sizing-table-view-cells.html

But I am having problems setting up a UIImageView using auto-sizing. I need the images to be different sizes in Landscape compared to portrait (so they retain the same aspect ratio). But each time I get a broken layout constraints error

"<NSLayoutConstraint:0x14554680 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x14564440(200)]>

As the actual code has quite a few different types of cell (that all work with auto-sizing cells) I have setup a simple example of the issue with a blue image view here:

https://github.com/AdrianMW/ios8autosizingCells

I have setup the constraints to the superview and am setting up the height like so:

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

    ImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kImageCell forIndexPath:indexPath];
    cell.heightConstraint.constant = [self heightForOrientation:self.interfaceOrientation];
    return cell;
}

Solutions Tried

I have tried turning off setTranslatesAutosizingMask: on the cell as per some of the other suggestions but when I do I just get a blank space instead of a image. I have also tried adding the following:

[cell setNeedsDisplay];
[cell layoutIfNeeded];

I have seen mention of overriding sizethatfits: to get it to work on this cell using the old way but that doesn't seem to be called on the cell.

Cheers

like image 406
atom Avatar asked Nov 23 '25 20:11

atom


1 Answers

In your ImageTableViewCell.xib, try changing the constraint:

Image View.Bottom EQUAL Superview.Bottom

Change the priority from 1000 to 999.

like image 198
John Martin Avatar answered Nov 28 '25 09:11

John Martin