I wanted to present an View with modal animation full screen on top the the existing view/viewcontroller programmatically(without button tap or tap gesture).
Is it possible to present a view without button tap/tap gesture?
I wanted to present an View with modal animation full screen on top the the existing view/viewcontroller programmatically(without button tap or tap gesture).
Is it possible to present a view without button tap/tap gesture?
there are many ways to do this, this is one way:
import SwiftUI @main struct TestApp: App { var body: some Scene { WindowGroup { ContentView() } } } struct ContentView: View { @State var goToIt = false var body: some View { NavigationView { NavigationLink(destination: Text("Second View"), isActive: $goToIt) { Text("Second view in 3 seconds") } }.onAppear { // simulating another process that triggers the change DispatchQueue.main.asyncAfter(deadline: .now() + 3) { goToIt = true } } } }
let swiftUIController = UIHostingController(rootView: ContentView(showModal: .constant(true))) rootController()!.present(swiftUIController, animated: true, completion: nil)
Above code works but not sure whether its a best practice.