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

21k views Asked by At

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?

5

There are 5 answers

3
Duyen-Hoa On BEST ANSWER
  1. UICollectionView is like a grid, UIStackView is only for 1 dimension: vertical or horizontal.

UICollectionView is like UITableView, but it supports more than single-column layouts.

Collection views provide the same general function as table views except that a collection view is able to support more than just single-column layouts. Collection views support customizable layouts that can be used to implement multi-column grids, tiled layouts, circular layouts, and many more. You can even change the layout of a collection view dynamically if you want.

vs

The UIStackView class provides a streamlined interface for laying out a collection of views in either a column or a row

  1. For me, With StackView, you benefit the "AutoLayout" feature, for example: you put 4 views in the Stack, this component will decide how those views will be presented on the screen, depending on their size.
0
Wain On

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.

0
Brian Broom On

Collection views are much more complicated UI elements, requiring a DataSource object, and a layout etc.

Stack Views seem to be a replacement for tricks where you had to apply multiple constraints to several elements to do (say) a stack of buttons, or arrange labels and text fields for a form.

Stack Views also don't have a delegate, and have no mechanism for selecting items or anything like that.

0
ejkujan On

Collection views have cells and work with data presentation. Stack views are a way to layout views within a container. Stack views do not have a way to work with the data as collection views do with delegates. Collection view is data centered and stack views are layout focused.

0
kurtanamo On

If you asking yourself why this 2 options are there, which looks like very similar - sometimes you'll create a custom Cell (tableview or collectionview) and will add a another tableview or a collectionview as a subview - then you will love to know how UIStackView works....