In my application I start with a LoginView which makes an API call to the backend for authentication. In case this is successfull (in a completion handler, i want to show the FeedView(). With UIKit this is all pretty simple trying to figure out how to do this with SwiftUI. So How can I show a new View programmatically (I don't have a NavigationView)?
CodePudding user response:
You can have a conditional view like so:
struct MakeSureUserIsLoggedInView: View {
@State private var loggedIn = false
var body: some View {
if loggedIn {
FeedView()
} else {
LoginView { // your login view with completion handler
loggedIn = true
}
}
}
}
Notice how we use @State to switch between what view is shown.
CodePudding user response:
If you don't want to use a NavigationView, you have several options:
A SwiftUI-native style would be simply to have a parent view which holds your view, and other views. You can have a property on a shared
ViewModeland the parent view can return a different child view depending on the value of the view modelDisplaying a new view modally (this is easiest)
Using UIKit navigation - this is my preferred method in complex apps where you can simply navigate around the app using UIViewControllers, with a SwiftUI view pinned to the edges
I can go into more detail on any of these, with some sample code from my own projects, if you would like. Option 1 is probably the most natural for SwiftUI and you can even animate the transitions pretty nicely with things like matchedGeometryView if you use this option.
