I need to create UICollectionView cell have full height but the dynamic width to fit a UIImageView's image. The UIImageView's constraints equal to cell ( contentView ). I have read about the dynamic height in this so my question is: "Is the AutoLayout allow autolayout dynamic width ?" .
1 Answers
To my knowledge UICollectionView doesn't handle width automatically. However you can easily set the width via the delegate. I refer you to this post : Dynamic cell width of UICollectionView depending on label width
It was UILabels instead of UIImageViews but the method should be similar using UIImage size property instead of sizeWithAttributes of NSString.
EDIT : After some tests it turns out UICollectionView does handle cell width automatically the same way as automatic height. So the delegate is not necessary. (see this tutorial for detailed info)
However, if the images are not available immediately (remotely fetched for instance) then you should wait until all images are downloaded and then display the collection. Otherwise you will need to invalidate the layout after each image is displayed which can lead to visual glitches.

- 458
- 4
- 11
-
I already read this post. But my problem is the images is set after init(). So I can not use delegate. – ông Hoàng 1 đêm Apr 08 '20 at 14:51
-
I've edited my answer. In your case you should probably wait until your images are ready before adding your collection view (or call reload data once your images are ready). – Bioche Apr 08 '20 at 18:57
-
1You're right. This problem is resolve by call reload data. Many thanks – ông Hoàng 1 đêm Apr 10 '20 at 07:56