Modifiers

In SwiftUI, modifiers are like superpowers you can add to your views. They’re methods with behavior provided by the View protocol letting you customize the appearance, behavior, and even the layout of any SwiftUI view in creative and flexible ways. Think of them as add-on features or enhancements you can apply to your views.

Configure a View with a Modifier

You add a modifier directly to a view using a dot (.) followed by the modifier’s name. For example:

Text("Hello, Kodeco!")
  .foregroundStyle(.blue) // Changes the text color

Modifiers don’t change the original view. They return a new, modified version, leaving the original intact.

Chaining Modifiers

The true power of modifiers comes from chaining them together. Build upon each customization by adding additional modifiers to create more complex customizations.

Text("Hello, Kodeco!")
  .font(.largeTitle)
  .foregroundStyle(.blue)
  .padding()

The order in which you apply modifiers matters, because each modifier builds on the previous one.

View-Specific Modifiers

Some SwiftUI views offer specialized modifiers unique to their type. These provide convenient ways to customize those particular views. Here are some examples:

  • Image: The .resizable() modifier controls how an image scales. You can use it only on Image views.

  • Text: The .bold() modifier adds a bold font weight. You can use it only on Text views.

See forum comments
Download course materials from Github
Previous: Introduction: Modifiers & State Next: Modifiers: Hands-On Demo