Search code examples
iosswiftuistackview

Whats element can I change to replace UIStackView to show 2 images row


I use a vertical UIStackView to show images in my iOS app just like the image below

image with other images in a column

But now I need to change that behaviour and show two images per row just like the example below.

image with other two images per row

What UI component can I use to do that ?

ps: I use ViewCode to create that screen, but I suppose it's not necessary to publish the code because I just want to know which component will be better to complete this task


Solution

  • You have a vertical UIStackView. You want a vertical stack view that contains 2 side-by-side images, right? So create a vertical stack view that contains horizontal stack views. Then you'll have vertical grouping of rows.

    Edit:

    If you want a true grid of images, you should look at using a UICollectionView instead.

    There are pros and cons to each approach. A vertical stack view containing horizontal stack views is simple and easy to build, and each row is a discrete object in the UI (if that's what you want.)

    A collection view is very powerful, but also complex and kind of confusing to manage. (If you haven't used one before, be prepared for a pretty steep learning curve. It makes a UITableView seem simple by comparison). It will let you set up a true grid of UI elements (or just about any other arrangement that you want.