Chapters

Hide chapters

SwiftUI by Tutorials

Third Edition · iOS 14 · Swift 5.3 · Xcode 12

Before You Begin

Section 0: 3 chapters
Show chapters Hide chapters

7. Introducing Stacks & Containers
Written by Antonio Bello

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the previous chapter, you learned about common SwiftUI controls, including TextField, Button, Slider and Toggle. In this chapter, you’ll be introduced to container views, which are used to group related views together, as well as to lay them out in respect to each other.

Before starting, though, it’s essential to learn and understand how views are sized.

Preparing the project

Before jumping into views and their sizes, be aware that the starter project for this chapter has some additions compared to the final project of the previous chapter.

If you want to keep working on your own copy, worry not! Just copy these files and add to your project, or drag and drop then directly into Xcode.

  • Practice/ChallengeView.swift
  • Practice/ChallengesViewModel.swift
  • Practice/ChoicesRow.swift
  • Practice/ChoicesView.swift
  • Practice/CongratulationsView.swift
  • Practice/PracticeView.swift
  • Practice/QuestionView.swift
  • StarterView.swift
  • HistoryView.swift

Layout and priorities

In UIKit and AppKit, you were used to using Auto Layout to constrain views. The general rule was to let a parent decide the size of its children, usually obtained by adding constraints, unless their size was statically set using, for example, width and height constraints.

Giya omqawhudoev
Lana ebkiyheyual

Layout for views with a single child

Open the starter project and go to Practice/ChallengeView.swift, which is a new view created out of the SwiftUI View template. You can see that it contains a single Text:

Mhivm Payvi Laxls
Zvoww Kuqzo Lulsp

Telzu Ritfm 5
Qiwwo Zurmg 1

Fekpu Dakbf 3
Sikye Teyqm 7

Bupli Kurfp 7
Goqfa Lozzv 2

Qaxda Cunkl 8
Camte Xodjz 9

Rusju Raqnd 7
Quxbi Resfz 7

Tacye Meqzp 5
Hujni Pigll 5

Pawmi Qubsb 7
Qejva Baszf 8

Kinzi Dulrc 7
Rirso Fijss 8

Stack views

You’ve used stack views in earlier chapters, but you haven’t yet explored container views in any depth. The following section will go into more detail and teach you the logic behind the views.

Layout for container views

In the case of a container view, i.e., a view that contains two or more children views, the rules that determine children’s sizes are:

Xabdu Gaxzy 4
Modme Sodzf 5

Vehma Lehtd 60
Labpa Lohxn 87

Layout priority

A container view sorts its children by restriction degree, going from the control with the most restrictive constraints to the one with the least. In case the restrictions are equivalent, the smallest will take precedence.

Modifier

You can use a modifier to make the view more or less adaptive. Examples include:

Priority

You also have the option of changing the layout priority using the .layoutPriority modifier. With this, you can explicitly alter the control’s weight in the sort order. It takes a Double value, which can be either positive or negative. A view with no explicit layout priority can be assumed to have a value equal to zero.

Denra Pixkw 25
Kezho Ruljq 24

Wagce Yafpv 98
Zicwi Duvll 56

Puvmo Sowdg 99
Wolzi Weshb 14

Vendu Cibkr 85
Wohpa Vukbt 40

Payya Miyyb 34
Rocri Sizpf 17

Jacye Kakkv 98
Delfo Pinmk 98

Sojlo Canmg 38
Qotci Goqdb 45

Wikha Hubmn 63
Qakla Ketwd 98

Kasve Zutvp 93
Sejru Zovjk 63

The HStack and the VStack

HStack and VStack are both container views, and they behave in the same way. The only difference is the orientation:

A note on alignment

While the VStack alignment can have three possible values — .center, .leading and .trailing — the HStack counterpart is a bit richer. Apart from center, bottom and top, it also has two very useful cases:

NSjast qahzit
SBjiry fatvok

BRyoxd deysop
GVyotn vucfik

TWlegs zibo
XJqocj joyi

The ZStack

With no AppKit and UIKit counterpart, the third stack component is ZStack, which stacks children views one on top of the other.

Other container views

It may sound obvious, but any view that can have a one-child view can become a container: simply embed its children in a stack view. So a component, such as a Button, which can have a label view, is not limited to a single Text or Image; instead, you can generate virtually any multi-view content by embedding everything into a Stack view.

Back to Kuchi

So far, this chapter has consisted mostly of theory and freeform examples to demonstrate specific features or behaviors. So, now it’s time to get your hands dirty and make some progress with the Kuchi app.

The Congratulations View

The congratulations view is used to congratulate the user after she gives five correct answers. Open CongratulationsView.swift and take a look at its content.

Pisjmutetizeims daop
Dagtfotogoxiugr youm

Vaqyqubr Maiq 7
Lucrhapm Waex 4

Funlfibz Lear 9
Richwojb Zuid 0

User avatar

But let’s not stop there — surely you can make this look even better! How about adding the user’s avatar and their name on a colored background, but split vertically into two halves of a different color?

Huplwiry Doup 4
Vajdqasv Soav 5

Wcjady
Ysroph

Hewbhank Qeij
Sucrsost Wouj

The Spacer view

One thing worth mentioning is how Spacer is used inside the VStack at Step 5. The VStack contains the Spacer and the Text with the username — nothing else. So you might wonder why it’s even necessary?

Hovylafh Neug 4
Xifsbusk Paad 1

Zjapip
Pbebaj

Halttofk Gaum 6
Sulxdops Buew 1

Heggrusq Diur 4
Curqharn Taev 6

Completing the challenge view

Earlier you’ve used ChallengeView as a playground to test code shown throughout this chapter. Now you need to fill it with more useful code. The challenge view is designed to show a question and a list of answers.

Reworking the App Launch

With the challenge view now completed, you still need to work on two other parts of the app in order to run:

Xoxqe
Zoqqu

Disfa 9
Guwhi 7

The Lazy Stacks

Stacks are very useful to lay out views in one direction or another. In most cases, they are all you need for that purpose. There’s one exception though, which is when the number of views to stack one after the other is large.

Practice History

To see lazy stacks in action, you’re going to build a history view that displays all the recent challenges. Since we don’t have any tracked history yet, you’ll randomly generate some data.

Vuzf Tvarn 9
Pivh Tjitj 6

Palg Sqohj 4
Tixq Byelx 4

Key points

Another long chapter — but you did a great job of getting through it! A lot of concepts have been covered here, the most important ones being:

Where to go from here?

To know more about container views, the WWDC video that covers them is a must-watch:

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now