Setting UICollectionViewCell size when cell is using AutoLayout

自闭症网瘾萝莉.ら 提交于 2019-12-11 11:03:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!