SwiftUI Views & the body Property

Reviewed & published by Brayan K

SwiftUI builds every screen from views: small value types that describe what the UI should look like. In this lesson you'll meet the View protocol, its body property, the core views Text, Image, and Button, and how to compose them into rich layouts.

Part of the free Swift course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.

Building iOS Apps with SwiftUI • Intermediate

What You'll Learn in This Lesson

1️⃣ The View Protocol & body

A SwiftUI view is any type that conforms to the View protocol. The protocol has exactly one requirement: a computed property called body that returns some View. The some keyword is an opaque type — it means "one specific view, whose exact type you don't need to spell out".

import SwiftUI

// Every SwiftUI screen is a TYPE that conforms to the View protocol.
// The protocol has ONE requirement: a computed 'body' property.
struct WelcomeView: View {
    // 'some View' is an OPAQUE type: it means "one specific kind of View,
    // but you (the caller) don't need to know which one."
    var body: some View {
        Text("Hello, SwiftUI!")
    }
}

// Text is a built-in view that shows a line of read-only text.
// Image shows a picture or an SF Symbol.
struct LogoView: View {
    var body: some View {
        Image(systemName: "swift")
    }
}

2️⃣ Composition — Building Big Views from Small Ones

You rarely build a screen as one view. Instead you compose: nest Text, Image, and Button inside container views like VStack, and extract reusable chunks into their own View structs. Each piece stays small, focused, and reusable.

3️⃣ Modifiers — Styling Views

A modifier is a method like .font() or .padding() that returns a new view wrapping the original with the change applied. You chain them. Order matters: each modifier acts on the result of the one before it.

import SwiftUI

// MODIFIERS return a NEW view that wraps the original with a change applied.
// You chain them; each one builds on the one before it.
struct StyledView: View {
    var body: some View {
        Text("Tap to continue")
            .font(.title)               // bigger font
            .foregroundColor(.blue)     // blue text
            .padding()                  // space around it
    }
}

// Order matters: .padding() then .background() pads first, THEN colours the
// padded area. Swap them and you'd colour the text first, then pad outside.
struct BadgeView: View {
    var body: some View {
        Text("New")
            .padding()
            .background(Color.green)
    }
}

Your turn. Fill in the blanks to declare a proper view and show a star above a welcome message.

// 🎯 YOUR TURN — fill in each ___ then preview it in Xcode.
import SwiftUI

struct GreetingView: ___ {        // 👉 the protocol every view conforms to
    var body: ___ View {          // 👉 the opaque keyword before 'View'
        VStack {
            // Show an SF Symbol named "star.fill"
            Image(systemName: "star.fill")
            // Show the text "Welcome back!"
            Text("___")            // 👉 your message
        }
    }
}

// ✅ When complete this shows a star above a welcome message.

Pro Tips

Common Errors (and the fix)

Mini-Challenge: Business Card

Build a BusinessCardView that stacks a profile SF Symbol, a name in a large bold font, a job title, and a tappable "Email me" button. Style it with at least two modifiers. Extract the whole card into its own View struct and use it inside a screen view.

🎉 Lesson Complete!

Practice quiz

What must every SwiftUI view conform to?

  • The View protocol
  • The UIView class
  • The Renderable protocol
  • The Drawable class

Answer: The View protocol. A SwiftUI view is any type that conforms to the View protocol.

What is the single required member of the View protocol?

  • A render() method
  • A draw(rect:) method
  • A computed body property
  • An init() method

Answer: A computed body property. Conforming to View means providing a computed body property that returns the view's content.

What type does the body property return?

  • Void
  • An opaque some View
  • A UIView
  • A String

Answer: An opaque some View. body is declared as 'var body: some View', an opaque return type describing one concrete view.

How do you build complex UIs in SwiftUI?

  • By subclassing UIView
  • By composing small views together
  • By editing a XIB file
  • By writing one giant view

Answer: By composing small views together. SwiftUI favours composition: you nest and combine small, focused views to build larger ones.

Which view displays a line of read-only text?

  • Label
  • TextView
  • Text
  • StringView

Answer: Text. Text is the SwiftUI view used to show a string of read-only text.

What does the 'some' keyword in 'some View' indicate?

  • An array of views
  • An optional view
  • An opaque type that is one specific View
  • Any random type

Answer: An opaque type that is one specific View. 'some View' is an opaque return type: a single concrete type that conforms to View, hidden from the caller.

Which view is used to show an image asset?

  • Picture
  • Photo
  • Bitmap
  • Image

Answer: Image. Image displays images, e.g. Image("logo") or Image(systemName: "star").

How do you create an SF Symbol image?

  • Image(symbol: "star")
  • Image(systemName: "star")
  • Symbol("star")
  • Image.system("star")

Answer: Image(systemName: "star"). Image(systemName:) loads one of Apple's built-in SF Symbols by name.

What is a view modifier?

  • A way to delete a view
  • A separate protocol
  • A storage property
  • A method that returns a new modified view

Answer: A method that returns a new modified view. Modifiers like .padding() or .foregroundColor() return a new view wrapping the original with the change applied.

Why are SwiftUI views described as a description of the UI?

  • They are mutable objects you update by hand
  • They are lightweight value types SwiftUI uses to compute the screen
  • They are stored in a database
  • They run on a background thread only

Answer: They are lightweight value types SwiftUI uses to compute the screen. Views are cheap value-type descriptions; SwiftUI reads them to figure out what to render and updates them efficiently.

Continue this course

Frequently asked questions

What is a View in SwiftUI?

A View is any Swift type (usually a struct) that conforms to the View protocol. The protocol has a single requirement: a computed body property describing what the view looks like. Views are lightweight value types — SwiftUI reads them to work out what to render and re-computes them efficiently when data changes, so you never manually update the UI.

What does the body property do?

body is a computed property of type 'some View' that returns the content of your view. SwiftUI calls body to build the view hierarchy. Because it returns a value each time, you describe what the UI should be for the current state, and SwiftUI handles drawing and updates.

What does 'some View' mean?

'some View' is an opaque return type. It tells the compiler the body returns one specific concrete type that conforms to View, without naming that type explicitly (it might be a deeply nested VStack<TupleView<...>>). It keeps signatures clean while preserving the exact type for SwiftUI's diffing.

How do I build a complex screen?

Through composition. You combine small views — Text, Image, Button — inside container views like VStack, HStack, and ZStack, and you extract reusable pieces into their own View structs. Each piece stays small and testable, and you nest them like building blocks.

What is a view modifier?

A modifier is a method such as .padding(), .font(), or .foregroundColor() that returns a new view wrapping the original with a change applied. You chain modifiers to style and configure views. Order matters: each modifier acts on the result of the previous one.