Mobile development

Stage 2 · Lesson 3 of 10

Modifiers and their order

SwiftUI views and layout

such as .padding(), .font(.headline), .foregroundStyle(.secondary) and .background() each return a new view that wraps the one before. So the order matters. Text("Hi").padding().background(.blue) colours the padded area.

.background(.blue).padding() colours only behind the text and leaves a clear gap around it. The same goes for .frame and .onTapGesture: a tap only registers on the area the modifier wraps, which may be smaller than it looks. Some modifiers, like .font, pass down to child views in a stack. To reuse a set of modifiers, write a custom ViewModifier or a view extension.

Common trapstacking modifiers until it looks right without understanding why.

A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock

Resources for this stage

Start with· follow one of these

  • CourseFree
    SwiftUI Tutorials (Apple)

    Apple · Beginner

    Apple's own path from views and stacks to lists and navigation

  • CourseFree
    100 Days of SwiftUI

    Paul Hudson (Hacking with Swift) · Beginner

    Days 16 to 35 build six small apps on views, modifiers, lists and animation

Practise

  • Use a free challenge's mobile design image as the spec for a native screen

Keep handy

Mark this stage as not done?