How to show a View depending on a condition in Swiftui

Asked

Viewed 42 times

1

In my project with swiftui I would like to present a view depending on the login status of my users.

var body: some View {
    HStack() {
        HomeView()
        LoginView()
    }
}

HomeView should appear if the user is logged in and LoginView otherwise.

1 answer

2


You can use the @ViewBuilder to achieve this:

@ViewBuilder
var body: some View {
    if user.isAuthenticated {
        HomeView()
    } else {
        LoginView()
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.