Views and the View Protocol
Every visible element in SwiftUI — from a single Text label to an entire screen — conforms to the View protocol. The protocol's sole requirement is a computed property named body that returns some View, describing what should be displayed. Because body itself returns another view (which may itself be composed of further views), SwiftUI interfaces are built as trees of small, composable pieces rather than a handful of large, monolithic view classes as is common in UIKit.
Cricket analogy: Every player on the field, from a fielder to the captain, conforms to a shared role like the View protocol; each has a 'body' of responsibilities, and a whole team's strategy is built by composing individual player roles, not one monolithic plan.
Composing Views
The idiomatic SwiftUI style favors extracting small, single-purpose views rather than writing one enormous body. A ProfileHeader view might combine an Image, a Text, and a Button — and that composed view can then be reused inside a ProfileScreen, a SettingsScreen, or anywhere else it's needed. This composability is enabled by the fact that every custom view is itself just another type conforming to View, indistinguishable from SwiftUI's own built-in views like Text or Image from the framework's perspective.
Cricket analogy: Instead of one player trying to bat, bowl, and field all at once, a captain like Rohit Sharma composes a team from specialists — an opener, a spinner, a wicketkeeper — reused across matches, just as small views compose into a screen.
struct ProfileHeader: View {
let username: String
let avatarSystemName: String
var body: some View {
VStack(spacing: 8) {
Image(systemName: avatarSystemName)
.resizable()
.frame(width: 64, height: 64)
.clipShape(Circle())
Text(username)
.font(.title2.bold())
}
}
}
struct ProfileScreen: View {
var body: some View {
VStack {
ProfileHeader(username: "ada.codes", avatarSystemName: "person.crop.circle")
Text("Joined 2024")
.foregroundStyle(.secondary)
}
.padding()
}
}Why `body` Returns `some View`, Not `View`
You cannot write var body: View directly, because View has an associated-type requirement (Body) that makes it unusable as a plain return type — Swift needs to know the concrete type at compile time for performance and type-checking. some View resolves this: it tells the compiler 'there is one specific, concrete type here, infer it, but hide it from callers.' This is why every path through a body implementation (e.g. both branches of an if) must return the same concrete view type, or SwiftUI wraps them using constructs like _ConditionalContent behind the scenes via @ViewBuilder.
Cricket analogy: You can't say 'the team will field one specific undetermined formation' without naming it; 'some View' is like a captain saying 'trust me, there's one specific fielding plan, I just won't announce it publicly' before the toss.
body's implicit @ViewBuilder attribute is what allows you to list multiple views inside a VStack { ... } without explicit array syntax or return statements — it's a Swift result builder that stitches sibling views into a single composed type.
A common beginner mistake is writing conditional logic across incompatible view types (e.g. returning Text in one branch and Image in another) without realizing @ViewBuilder handles this via _ConditionalContent — this works fine in practice, but forgetting it exists can cause confusion when reading compiler-generated type errors.
- The
Viewprotocol requires only abodyproperty describing what to render. - SwiftUI interfaces are trees of small, composable custom views, not monolithic screen classes.
- Extracting reusable subviews (like a
ProfileHeader) is idiomatic SwiftUI style. bodyreturnssome View, an opaque type, becauseViewhas an associated type and can't be used as a direct return type.@ViewBuilder(implicitly applied tobody) lets you list sibling views without explicit array or return syntax.- Custom views are indistinguishable from built-in views like
Textfrom SwiftUI's perspective.
Practice what you learned
1. What is the sole required property of the `View` protocol?
2. Why can't `body` be declared as returning plain `View`?
3. What Swift feature allows multiple sibling views to be listed inside `body` without explicit array syntax?
4. What is the idiomatic SwiftUI approach to building a complex screen?
5. From SwiftUI's perspective, how does a custom view like `ProfileHeader` compare to a built-in view like `Text`?
Was this page helpful?
You May Also Like
What Is SwiftUI?
An introduction to Apple's declarative UI framework — how it differs from UIKit, its core mental model, and why state drives the interface.
Swift Essentials for SwiftUI
The core Swift language features — structs, protocols, closures, optionals, and property wrappers — that every SwiftUI developer needs to be fluent in.
Modifiers in SwiftUI
How view modifiers work under the hood, why order matters, and how chained modifiers build up new wrapped view types.
Stacks: VStack, HStack, and ZStack
How SwiftUI's three core layout containers arrange child views vertically, horizontally, and by depth, plus alignment and spacing controls.