SwiftUI Views & Modifiers Questions with Answers

12 practice questions on SwiftUI Views & Modifiers (iOS (Swift & SwiftUI)) with answers and step-by-step solutions. Try 10 samples below, then practise all 12 free in the AptiNest app.

Sample questions

Question 1Easy

What does it mean that SwiftUI is declarative?

  1. You must declare every view's size in advance
  2. You lay out every screen in storyboard files
  3. You describe what the UI should look like for a state
  4. You write step-by-step code to add and remove views

Answer: C. You describe what the UI should look like for a state

Explanation

In SwiftUI the UI is a function of state. When state changes, SwiftUI calls body again and updates the screen to match.

Step-by-step solution

  1. SwiftUI focuses on what the UI should look like, not how to build it.
  2. You describe the desired appearance based on current state.
  3. When state changes, SwiftUI automatically updates the view to reflect the new state.
  4. This means the UI is a function of state and reacts to it.
  5. The correct answer is You describe what the UI should look like for a state.

Why the other options are wrong

  • A. Declaring every view's size in advance is not required; SwiftUI sizes views from their content and layout.
  • B. Storyboards are a UIKit tool; SwiftUI views are described in code, not laid out in storyboard files.
  • D. Step-by-step add and remove code is the imperative style, the opposite of declarative.

Tip: Declarative means describing the end goal, not the process.

Question 2Easy

Which container overlays its children on top of each other?

  1. VStack
  2. ZStack
  3. HStack
  4. LazyVGrid

Answer: B. ZStack

Explanation

VStack arranges children vertically, HStack horizontally, and ZStack layers them along the z-axis (depth).

Step-by-step solution

  1. ZStack places its children on top of each other along the depth axis.
  2. VStack stacks children vertically, HStack horizontally, and LazyVGrid lays them out in a grid.
  3. Marked answer: option B, ZStack.

Why the other options are wrong

  • A. VStack stacks children vertically, not on top of each other.
  • C. HStack stacks children horizontally, side by side.
  • D. LazyVGrid lays children out in rows and columns, not on top of each other.

Tip: ZStack is the Z axis (overlay); VStack is vertical; HStack is horizontal.

Question 3Easy

What does Spacer() do inside an HStack?

  1. It adds a fixed 8-point gap between the two views around it
  2. It hides the views that come after it
  3. It expands to fill the available space along the stack's axis
  4. It draws a thin divider line

Answer: C. It expands to fill the available space along the stack's axis

Explanation

Spacer is flexible: it takes up all free space, pushing the other views apart, for example to the leading and trailing edges.

Step-by-step solution

  1. Spacer is a flexible view.
  2. It expands to fill free space along the stack's axis.

Why the other options are wrong

  • A. It is not fixed size.
  • B. It does not hide views.
  • D. Divider draws lines.

Tip: Spacer = flexible gap.

Question 4Easy

What is the main purpose of a #Preview block in a SwiftUI file?

  1. To define the screen shown when the app launches
  2. To show the view in the App Store listing
  3. To render the view live in Xcode's canvas
  4. To write unit tests for the view

Answer: C. To render the view live in Xcode's canvas

Explanation

Previews let Xcode render a view, often with sample data, without running the full app. They are not part of the shipped UI.

Step-by-step solution

  1. #Preview is a macro that tells Xcode's canvas how to show the view.
  2. The canvas renders the view live while you edit, without running the whole app.
  3. Marked answer: option C.

Why the other options are wrong

  • A. The launch screen is set in app settings, not in a #Preview block.
  • B. The App Store listing is managed in App Store Connect, not in code.
  • D. Unit tests are written with a testing framework, not with #Preview.

Tip: Use #Preview to see SwiftUI views live while you code.

Question 5Medium

When is LazyVStack a better choice than VStack?

  1. When the stack should ignore the safe area
  2. For short forms, since it lays out every child much faster
  3. When the children must overlap each other
  4. Long scrolling content, since rows are built on demand

Answer: D. Long scrolling content, since rows are built on demand

Explanation

VStack creates all its children at once. Inside a ScrollView with many rows, LazyVStack builds rows as they come on screen, saving memory and time.

Step-by-step solution

  1. LazyVStack builds items as they are needed, saving memory.
  2. VStack creates all children at once, which uses more memory.
  3. Long scrolling content needs less memory with LazyVStack.
  4. LazyVStack is better for long lists or scrollable views.
  5. Rows are built on demand in LazyVStack, improving performance.
  6. Final answer: Long scrolling content, since rows are built on demand.

Why the other options are wrong

  • A. The safe area is handled by modifiers such as ignoresSafeArea, not by choosing LazyVStack.
  • B. Lazy stacks do not lay out short forms faster; for a few children VStack is fine.
  • C. Overlap comes from ZStack, not LazyVStack, which stacks children in a column.

Tip: Use LazyVStack for long scrolling content to save memory and improve speed.

Question 6Medium

How do Text("Hi").padding().background(.red) and Text("Hi").background(.red).padding() differ?

  1. First: padding is red too; second: only behind the text
  2. The second paints the whole screen red
  3. They look identical, since modifier order does not matter
  4. The first adds no padding at all to the text

Answer: A. First: padding is red too; second: only behind the text

Explanation

Each modifier wraps the view before it. Padding then background colours the padded area; background then padding colours only the text's frame and adds clear space round it.

Step-by-step solution

  1. First: padding is added first, then the background fills the padded area, so the padding is red.
  2. Second: the background fills only the text, then padding adds clear space around it.
  3. Marked answer: option A.

Why the other options are wrong

  • B. Background only fills the view it is attached to, not the whole screen.
  • C. Modifier order changes the result, so the two look different.
  • D. Both versions add padding; the first one's padding is simply coloured red.

Tip: Each modifier wraps the view before it, so order decides what gets painted.

Question 7Medium

Why can a view's body contain if/else branches that return different view types?

  1. some View allows a different concrete type for every call
  2. SwiftUI converts every view to AnyView at runtime
  3. body is a @ViewBuilder, which merges branches into one type
  4. Each branch must be wrapped in a Group, or it will not compile

Answer: C. body is a @ViewBuilder, which merges branches into one type

Explanation

The @ViewBuilder result builder turns if/else into a single conditional view type, so body still returns one concrete type.

Step-by-step solution

  1. The body of a view is built with @ViewBuilder, which turns if/else branches into one conditional view type.
  2. The compiler combines the branches into a single type, so the return type stays fixed.
  3. Marked answer: option C.

Why the other options are wrong

  • A. Some View stands for one fixed concrete type for that place in the code; it does not change per call.
  • B. SwiftUI does not convert every view to AnyView; that is only used when type erasure is asked for.
  • D. A Group is not needed for if/else to compile; @ViewBuilder handles the branches.

Tip: If/else inside a view body works because of @ViewBuilder, so no Group is needed.

Question 8Medium

ForEach(items) { item in Text(item.name) } requires what of the items?

  1. They are classes rather than structs
  2. Identifiable conformance, or an id: key path
  3. They conform to Codable
  4. They are stored in a Set rather than an Array

Answer: B. Identifiable conformance, or an id: key path

Explanation

ForEach needs a stable identity for each element so it can track insertions, deletions and moves. Identifiable supplies an id, or you give one with id: \.someProperty.

Step-by-step solution

  1. ForEach needs a way to identify each item.
  2. Identifiable gives an id, or you pass id: keyPath.

Why the other options are wrong

  • A. Structs work.
  • C. Codable is unrelated.
  • D. Arrays are fine.

Tip: Identifiable or id:.

Question 9Hard

How do you make a button stretch to the full available width?

  1. Apply .frame(maxWidth: .infinity) before background
  2. Wrap the button in a ZStack
  3. Apply .padding(.infinity) to the button's text label
  4. Set .fixedSize() on the button

Answer: A. Apply .frame(maxWidth: .infinity) before background

Explanation

frame(maxWidth: .infinity) lets the view take all offered width. Putting it before background makes the background fill that whole width.

Step-by-step solution

  1. Apply .frame(maxWidth: .infinity) to set maximum width to infinity.
  2. This lets the button take full available width automatically.
  3. Putting it before background ensures background fills the whole width.
  4. Other options don't make the button stretch properly.
  5. Final answer is option A.

Why the other options are wrong

  • B. A ZStack only layers views; it does not stretch the button to full width.
  • C. padding(.infinity) is not a valid use; padding on the label would not widen the button.
  • D. fixedSize makes the button use its ideal size, so it does not expand.

Tip: For full-width buttons, use .frame(maxWidth: .infinity) before background.

Question 10Hard

What happens when a view gets a new value in its .id(_:) modifier?

  1. The view keeps its @State but replays its animations
  2. SwiftUI treats it as a new view and resets its state
  3. Only the view's colour scheme is refreshed
  4. Nothing, because .id only affects accessibility

Answer: B. SwiftUI treats it as a new view and resets its state

Explanation

Explicit identity tells SwiftUI which view is which. A different id means a different view, so the old one (with its @State) is removed and a fresh one is created.

Step-by-step solution

  1. The .id modifier sets the view's identity.
  2. When the id value changes, SwiftUI treats the view as a new one, so its @State starts again.
  3. Marked answer: option B.

Why the other options are wrong

  • A. A new identity means the old @State is not kept, so the view is not carried over.
  • C. The id modifier does not change colour scheme.
  • D. .id does change view identity, so it affects more than accessibility.

Tip: Change a view's .id to force a fresh view and reset its state.

Practise all 12 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all iOS (Swift & SwiftUI) topics · All topics