Conditional Views with If-Else Statements in SwiftUI
By RaiTech Labs · more summaries from this channel
3 min video·en··1265 views
Summary
The video explains how to use conditional statements in SwiftUI to show or hide views based on a state variable, demonstrating a login example.
Key Points
- —The presenter introduces conditional views in SwiftUI, building on the concept that a view's body can contain only one container view such as a VStack or ZStack.
- —An if‑else statement is placed inside the body to return different view hierarchies depending on the isLoggedIn state.
- —A @State variable named isLoggedIn is declared to track whether the user is logged in, holding a Boolean value.
- —A button placed outside the if‑else block remains visible regardless of the login state, allowing the user to toggle isLoggedIn.
- —When isLoggedIn is true, the view displays a rectangle and a text label inside a ZStack.
- —When isLoggedIn is false, the view displays a simple text prompting the user to log in.
- —Pressing the button updates the isLoggedIn state, causing SwiftUI to recompute the body and switch between the two conditional view branches.
- —The demonstration shows how conditional rendering lets developers show or hide UI elements based on Boolean conditions, a common pattern in SwiftUI apps.
- —The instructor notes that this technique will be used in upcoming lectures and encourages viewers to ask questions in the comments.
Copy All
Share Link
Share as image
Bookmark
More Resources
Get key points from any YouTube video in seconds
More Summaries

Claude Code built me a $273/Day online directory
55 min·en

GSP teaches Lex Fridman how to street fight
1 hr 49 min·en

Jordan Peterson: Life, Death, Power, Fame, and Meaning | Lex Fridman Podcast #313
3 hr 3 min·en

Elon Musk Makes Shocking Future Predictions At The World Economic Forum In Davos
7 min·en

What ACTUALLY Makes People Buy Things (Pricing Psychology Explained)
16 min·en