History
Loading...
Loading...
September 29, 2025
@ViewBuilder functions to clean up complex conditional view logic instead of cramming multiple conditions into your view body. Create dedicated functions like @ViewBuilder private func statusIcon() -> some View to handle different UI states - this makes your views more readable and testable.Breaking down complex conditional view logic into separate @ViewBuilder functions keeps your main view body clean and focused. Each function handles a specific aspect of the UI state, making the code more modular and easier to understand.
struct OrderStatusView: View {
let status: OrderStatus
var body: some View {
VStack {
statusIcon()
statusMessage()
}
}
@ViewBuilder
private func statusIcon() -> some View {
switch status {
case .pending:
Image(systemName: "clock.fill")
.foregroundColor(.orange)
case .processing:
Image(systemName: "gear")
.foregroundColor(.blue)
case .shipped:
Image(systemName: "truck.box")
.foregroundColor(.green)
case .delivered:
Image(systemName: "checkmark.circle.fill")
.foregroundColor(.green)
}
}
@ViewBuilder
private func statusMessage() -> some View {
switch status {
case .pending:
Text("Order received")
case .processing:
Text("Preparing your order")
case .shipped:
Text("On the way")
case .delivered:
Text("Delivered!")
}
}
}This approach improves code maintainability by separating concerns, makes unit testing easier since you can test individual view components, and reduces cognitive load when reading the main view body. It also makes it simpler to modify specific UI states without affecting the entire view structure.