UITableView dynamic cell height conflict - height NOT depending on a label
Asked Answered
S

2

6

So, my table view displays images. Every cell is basically an image filling out the cells entire contentView. As the images come with different aspect ratios, I need my cells to be adjusting their height depending on the table views width and the aspect ratio of the image. I've followed this Ray Wenderlich tutorial but now get a constraint conflict. The image is resized by altering the imageViews height constraint e.g. myImageViewHeight.constant = tableView.frame.width / aspectRatio

2016-06-16 13:56:25.823 MyApp[92709:5649464] 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. 
(
"<NSLayoutConstraint:0x7fbcdaf5c190 V:[UIImageView:0x7fbcdaf5c300(303)]>",
"<NSLayoutConstraint:0x7fbcdaf5b460 V:|-(0)-[UIImageView:0x7fbcdaf5c300]   (Names: '|':UITableViewCellContentView:0x7fbcdaf52230 )>",
"<NSLayoutConstraint:0x7fbcdaf5b4b0 V:[UIImageView:0x7fbcdaf5c300]-(0)-|   (Names: '|':UITableViewCellContentView:0x7fbcdaf52230 )>",
"<NSLayoutConstraint:0x7fbcdaf5e550 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fbcdaf52230(100)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fbcdaf5c190 V:[UIImageView:0x7fbcdaf5c300(303)]>

The image view has the following constraints and has the cells contentView as superview.

Constraints

In the table view controller class, I'm using

self.tableView.estimatedRowHeight = 80
self.tableView.rowHeight = UITableViewAutomaticDimension

I've also tried this - same result.

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

I'm guessing that I have to get rid of the 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fbcdaf52230(100)]>" but how? Also, does anybody know some kind of documentation/tutorials on how to properly create table views with dynamic content that not only cover having a bunch of labels inside the cell? My code works just fine if the image view is replaced by a label...

Secretive answered 16/6, 2016 at 12:42 Comment(5)
remove your imageview height constraint and after check. you are set top and bottom constraint that you are not set height equal constraint.Ambrosine
@Bhadresh How will the pictures get their proper height if the height constraint is removed?Secretive
you image fix height that remove top & bottom constraint and set center constraint.Ambrosine
@Bhadresh It solved the problem with conflicting constraints but now the imageView gets the same dimensions as the image itself(in pixels). If an image is 1000 px high, the image view get 1000px high. How do I tell the view to have the height equal to the table view width / the images aspect ratio?Secretive
you want imageview in width aspect ratio right?Crocker
A
15

You implementation is correct don't change anything in your code or any of your constraints.Removing the warning is easy just select the height constraint and lower its priority from 1000 to 999 it will fix your warning.If it still comes, let me know.

Animadvert answered 16/6, 2016 at 13:19 Comment(5)
Why should the priority be lowered?Eec
Check out this link for better understanding #43129097Animadvert
My question is why. I know what it does.Eec
For me, the above made the cell show up okay initially, but if you color the cell a translucent color and the tableview a different translucent color, you'll see that after viewDidLoad the tableViewCell height is still 44 pts and you can't interact with say a UISlider in it if it's outside that 44 pt height. If you do a reloadData on the tableView on viewDidAppear, it fixes this problem. I'm guessing apple tested dynamic height on cells with UILabels but not other UIViews and this is a bug.Misrepresent
This answer is correct. But why do we have to do this? it makes no sense that iOS adds an active height constraint for the UITableViewCellContentView. And then we have to do this hack. The hack seems to be necessary for image views only.Henn
E
0

It is an old question but I put this just for the record. The problem here is the estimated hight, 80, that is lower then the computed hight, 100. Changing the estimated hight to >=100 should fix this warning.

Eikon answered 28/10, 2019 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.