Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
5 of 8
tuned code
Asperi
  • 262.2k
  • 28
  • 594
  • 889

Here is an approach with which you can control navigation between your views with any transition/animation you wish (w/o UIKit representables)

Note: Transitions do not work in Preview (at least at my side, Xcode 11.2), so Simulator or real device should used for testing

If you expected something like the following

custom SwiftUI modal

Here is a simple code for demo the approach (of corse animation & transition parameters can be changed by wish)

struct ModalView : View { @Binding var activeModal: Bool var body : some View { VStack { Button(action: { withAnimation(.easeInOut(duration: 0.3)) { self.activeModal = false } }) { Text("Hide modal") } Text("Modal View") } .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .center) .background(Color.green) } } struct MainView : View { @Binding var activeModal: Bool var body : some View { VStack { Button(action: { withAnimation(.easeInOut(duration: 0.3)) { self.activeModal = true } }) { Text("Show modal") } Text("Main View") } .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .center) .background(Color.yellow) } } struct ModalContainer: View { @State var showingModal = false var body: some View { ZStack { MainView(activeModal: $showingModal) .allowsHitTesting(!showingModal) if showingModal { ModalView(activeModal: $showingModal) .transition(.move(edge: .bottom)) .zIndex(1) } } } } 
Asperi
  • 262.2k
  • 28
  • 594
  • 889