The View protocol lets you create what users see on the screen. The body property defines the content and layout of that view.
View protocol and body property in iOS Swift
Introduction
Syntax
iOS Swift
struct MyView: View {
var body: some View {
// UI elements here
}
}The body property must return exactly one view.
Use some View to tell SwiftUI you return a view without specifying the exact type.
Examples
iOS Swift
struct GreetingView: View {
var body: some View {
Text("Hello, friend!")
}
}iOS Swift
struct ColoredCircle: View {
var body: some View {
Circle()
.fill(Color.blue)
.frame(width: 100, height: 100)
}
}iOS Swift
struct CombinedView: View {
var body: some View {
VStack {
Text("Title")
.font(.largeTitle)
Text("Subtitle")
.foregroundColor(.gray)
}
}
}Sample App
This app shows a welcome message and a button. When you tap the button, it prints a message in the console.
iOS Swift
import SwiftUI struct ContentView: View { var body: some View { VStack(spacing: 20) { Text("Welcome to SwiftUI!") .font(.title) .padding() Button("Tap me") { print("Button tapped") } .buttonStyle(.borderedProminent) } .padding() } } @main struct MyApp: App { var body: some Scene { WindowGroup { ContentView() } } }
Important Notes
The body property is recomputed when state changes, so keep it fast and simple.
Every SwiftUI view must conform to the View protocol and implement body.
You can combine many views inside body using containers like VStack, HStack, and ZStack.
Summary
The View protocol defines what your UI shows.
The body property returns the view's content and layout.
Use simple SwiftUI views inside body to build your interface.
Practice
1. What does the
body property in a SwiftUI View protocol represent?easy
Solution
Step 1: Understand the role of the View protocol
The View protocol defines what the user interface shows in SwiftUI.Step 2: Identify the purpose of the body property
The body property returns the actual content and layout that the view displays.Final Answer:
The content and layout of the view -> Option DQuick Check:
body property = content and layout [OK]
Hint: Remember: body shows what the user sees [OK]
Common Mistakes:
- Confusing body with data or logic
- Thinking body controls navigation
- Assuming body is the app entry point
2. Which of the following is the correct way to declare the
body property in a SwiftUI View?easy
Solution
Step 1: Recall the View protocol requirement
The body property must be a computed property returning some View.Step 2: Check each option's syntax
var body: some View { Text("Hello") } correctly declares a computed property with type some View returning a Text view.Final Answer:
var body: some View { Text("Hello") } -> Option AQuick Check:
body is a computed property returning some View [OK]
Hint: body is always a computed property returning some View [OK]
Common Mistakes:
- Using func instead of var for body
- Returning a String instead of a View
- Declaring body as a stored property
3. Given the SwiftUI view code below, what will be displayed on the screen?
struct ContentView: View {
var body: some View {
VStack {
Text("Hello")
Text("World")
}
}
}medium
Solution
Step 1: Understand VStack layout
VStack arranges its child views vertically, one above the other.Step 2: Analyze the body content
The body contains a VStack with two Text views: "Hello" and "World".Final Answer:
Two texts stacked vertically: Hello above World -> Option CQuick Check:
VStack stacks views vertically [OK]
Hint: VStack stacks views vertically, HStack horizontally [OK]
Common Mistakes:
- Confusing VStack with HStack
- Expecting only one Text to show
- Thinking this causes an error
4. Identify the error in this SwiftUI view code:
struct MyView: View {
var body: View {
Text("Hi")
}
}medium
Solution
Step 1: Check the body property type
The body property must return 'some View' to satisfy the View protocol.Step 2: Identify the incorrect return type
The code uses 'View' instead of 'some View', which causes a compile error.Final Answer:
body must return 'some View', not 'View' -> Option AQuick Check:
body return type = some View [OK]
Hint: Always use 'some View' as body return type [OK]
Common Mistakes:
- Using 'View' instead of 'some View'
- Thinking Text needs parentheses
- Confusing property with function
5. You want to create a SwiftUI view that shows a red circle and a blue square stacked vertically. Which
body implementation correctly achieves this?hard
Solution
Step 1: Choose vertical stacking
Use VStack to stack views vertically as required.Step 2: Set correct colors and shapes
Circle filled with red and Rectangle filled with blue matches the requirement.Step 3: Verify frame sizes
Both shapes have 50x50 frames, ensuring visible size.Final Answer:
VStack with red filled Circle and blue filled Rectangle -> Option BQuick Check:
VStack + fill colors match requirement [OK]
Hint: Use VStack for vertical stack and fill() for colors [OK]
Common Mistakes:
- Using HStack instead of VStack
- Using stroke() instead of fill() for color
- Swapping colors of shapes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
