问题
I have a UICollectionViewCell that has a dynamic height. It contains a UIWebView that loads some content asynchronously. After load, it needs to resize itself to accommodate its contents.
I do the cell resizing by changing the height constraint of the web view.
I also send a delegate method to the collection view controller that the cell has resized and I need to invalidate layout.
However, I am getting auto layout errors about conflicting constraints. It seems the collection view is trying to resize the cell.
Does anyone know how to make this design work?
回答1:
A collection view works by the layout object specifying an explicit size for the cells. Similarly to table views, you can't really do the layout from the cells outward.
To implement your design you'd need to make the layout object aware of the size the cell needs to be based on its contents. There are several ways to achieve this, none of which are great. Probably in your case you'd need to pass the new content size back at the same time as you're invalidating the layout, and get the new size for that index path back to the layout somehow.
I'd also not change the height constraint on your web view. Keep it pinned to the edges of the cells content view, and let its size be determined by the size sent down from the layout object. That way, you're not going to get clashing constraints.
来源:https://stackoverflow.com/questions/21955578/setting-uicollectionviewcell-size-when-cell-is-using-autolayout