Conditionally use view in SwiftUI
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
SwiftUI provides several ways to conditionally display views: if statements inside view builders, the ternary operator for inline conditions, Group for conditional blocks, and custom View extensions for conditional modifiers. The @ViewBuilder attribute enables writing control flow directly in view code. Understanding when to use each approach ensures clean, performant UI code.
Basic if/else in View Builders
SwiftUI's @ViewBuilder allows if/else directly in the body property. Each branch can return different view types.
Showing or Hiding a View
Key difference: if removes the view entirely (no layout space). .opacity(0) hides it visually but preserves layout. Choose based on whether you want the hidden view to affect spacing.
Ternary Operator for Simple Conditions
The ternary operator is best for toggling between two values of the same modifier.
Conditional Modifier Extension
This extension conditionally applies modifiers without duplicating the entire view chain.
switch Statement
Optional Binding with if-let
Group for Conditional Blocks
AnyView for Dynamic Type Erasure
Prefer @ViewBuilder over AnyView — it preserves type information and enables better SwiftUI diffing.
Animated Conditional Views
Wrap the state change in withAnimation and add .transition() to the conditional view for smooth animations.
Common Pitfalls
- Using AnyView unnecessarily: Wrapping conditional views in
AnyViewdisables SwiftUI's structural identity and diff optimization. Use@ViewBuilderandif/elseinstead, which let SwiftUI efficiently track which branch is active and animate transitions. - Forgetting that if/else branches are different view identities: When an
if/elseswitches branches, SwiftUI treats them as entirely different views and recreates state. If both branches have aTextField, the text is lost on switch. Use.opacity()or.disabled()to preserve state when toggling visibility. - Deeply nested conditional logic in view body: Multiple nested
if/elsestatements make the view body hard to read and compile slowly. Extract conditional logic into computed properties or separate@ViewBuilderfunctions. - Not animating conditional view changes: Adding or removing a view with a plain
ifstatement causes abrupt appearance/disappearance. Wrap the state change inwithAnimation {}and add.transition(.opacity)to the view for smooth animations. - Conditional modifiers returning different types:
view.padding(condition ? 10 : 0)works, butview.if(condition) { $0.background(Color.red) }without@ViewBuildercauses type mismatches. Always use the@ViewBuilderattribute on functions that return conditional views.
Summary
- Use
if/elsein@ViewBuilderbodies for conditional views — this is the standard approach - Use the ternary operator for toggling modifier values (
color,font,padding) - Use
.opacity(0)or.hidden()to hide a view while preserving its layout space - Create a
Viewextension with@ViewBuilderfor conditional modifier application - Prefer
@ViewBuilderoverAnyViewto preserve SwiftUI's structural diffing - Wrap state changes in
withAnimationand add.transition()for animated conditional views

