@ViewBuilder before your body property to use if/else directly without wrapping views in HStack or VStack. For conditional modifiers, use a View extension with @ViewBuilder but be aware of type-erasure trade-offs.Why this happens
SwiftUI’s view builder system requires that the body property returns a single view type. When you write an if/else branch without @ViewBuilder, the compiler sees two different view types (TabView vs LoginView) and cannot reconcile them into one return type. Developers often work around this by wrapping the conditional in an HStack or VStack, which forces a single container type but adds unnecessary layout constraints.
The @ViewBuilder attribute tells the compiler to treat the property as a view-building closure that can emit multiple branches, merging them into a single TupleView behind the scenes. This is the same mechanism that powers SwiftUI’s built-in control flow like ForEach and Group.
How to fix it
1. Annotate body with @ViewBuilder (recommended)
The simplest fix is to mark your body property with @ViewBuilder. This lets you write plain if/else (or switch) statements directly:
@ViewBuilder
var body: some View {
if keychain.get("api-key") != nil {
TabView()
} else {
LoginView()
}
}
No extra container is needed. The compiler synthesizes a TupleView that holds either branch. This works for any number of branches and keeps the view hierarchy flat.
2. Use a View extension for conditional modifiers
When you need to conditionally wrap or modify an existing view (e.g., embed in a ScrollView or apply a color), a reusable extension keeps call sites clean:
extension View {
@ViewBuilder
func `if`<Content: View>(
_ condition: Bool,
@ViewBuilder transform: (Self) -> Content
) -> some View {
if condition {
transform(self)
} else {
self
}
}
}
Usage examples:
// Conditionally embed in a ScrollView
VStack {
Text("Line 1")
Text("Line 2")
}
.if(shouldScroll) { content in
ScrollView(.vertical) { content }
}
// Conditionally apply a modifier
Text("Some text")
.if(isError) { $0.foregroundColor(.red) }
Important caveat: This pattern returns some View but the underlying type differs between branches. SwiftUI handles this via type erasure, which can affect performance (re-rendering) and animation behavior. For details, see the objc.io article on conditional view modifiers.
3. Use Group for inline branching without @ViewBuilder
If you cannot annotate body (e.g., inside a computed property), wrap the conditional in Group:
var content: some View {
Group {
if user.isLoggedIn {
MainView()
} else {
LoginView()
}
}
}
Group is a transparent container that doesn’t affect layout, making it a lightweight alternative when @ViewBuilder isn’t an option.
How to prevent it
- Default to
@ViewBuilderonbodyfor any view that needs conditional branches. - Reserve the
.if()extension for modifier logic, not top-level view switching. - Avoid
AnyViewunless you truly need type erasure (e.g., heterogeneous arrays). - Profile with Instruments if you use conditional modifiers heavily; watch for unexpected re-renders.
FAQ
Does @ViewBuilder work with switch statements?
Yes. You can use switch on an enum or any equatable value inside a @ViewBuilder body, and each case can return a different view type.
Why does my animation break when using the .if() extension?
Because the two branches produce different concrete view types, SwiftUI cannot animate the transition between them. Use if/else directly inside @ViewBuilder with a shared container (like ZStack + opacity) for animatable transitions.
Can I use @ViewBuilder on functions other than body?
Absolutely. Any function or computed property that returns some View can be marked @ViewBuilder to enable multi-statement view construction with conditionals.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.