What's the difference between a UIStackView And A UICollectionView in Xcode 7?

nachshon f picture nachshon f · Jun 10, 2015 · Viewed 16.4k times · Source

What's the difference between a UIHorizontalStackView and a Collection View(Also Vertical stackview)?

Can't a collectionView be horizontal and vertical? Why would people use both?

What does a UIStackView do that a Collection View can't?

Answer

Wain picture Wain · Jun 10, 2015

Yes, a collection view using a flow layout or a custom layout can be vertical or horizontal. When using a flow layout it's pretty easy to configure a single column of items which are all set to their intrinsic content size.

The stack view is basically a trim and specialised version of that. It takes away the flexibility of the collection layout and in return gives you a streamlined interface.

Depending on your use case you may not need the complexity of a collection view. There are also some nice little features, like if you had a list of options to display but some aren't appropriate in all cases then you can just hide the ones that aren't and the stack view will deal with it. Hiding items in a collection view requires a good deal more configuration.

Stack views also form a very lightweight option for container views created entirely within interface builder and with no requirement for code. In this way the stack view is replacing a lot of your auto layout constraints by using the intrinsic content sizes of the subviews it manages to flow the layout. You can also very effectively nest stack views to form most tabular type layouts.