The problem with scrollable views (UIScrollView views as UICollectionViews) is that you have to deal with a scroll or pre-calculate the width of your content, which is not always easy. For this reason, if you do not want to have scrollable content, I would not use UICollectionView, nor any view based on UIScrollView.
Then you have the opportunity to switch from UIStackView. Stack views are good for "adding" several views and creating a kind of "heap" of views in a very simple way. However, if you do not control the number of items that you need, you will overcome the boundaries of your container view.
Therefore, this is what I would do:
"Container size with fixed container . " If your container view (your cell) has a fixed width (which never changes), I would manually add as many UIImageViews that I want to support in XIB and then hide / show them depending on the number of elements I want to display.
"Width of a container view with a variable width . " If your container view (your cell) has a variable width (which varies depending on the screen size or any other factor), then you will have to calculate in any case (do the math!) The number of objects that you can display within the width available you. You can then choose between using UIStackView or manually adding your views and constraints to the container view.
Does what I say make sense?
Thanks,
source share