7

Is there a way to dismiss a modal view without animation in SwiftUI?

I want to dismiss a modal without the dismiss animation because I want to navigate from the modal view to a new SwiftUI View using a view router. Everything is working, except for the transition animation from the modal view to the new full-screen view. I followed that tutorial to create a view router: Tutorial

I'm using that code snippet to present the modal view:

struct ContentView: View {

  @State private var showModal = false
  @Environment(\.presentationMode) var presentationMode


  var body: some View {
    Button(action: {
        self.showModal = true
    }) {
        Text("Show modal")
    }.sheet(isPresented: self.$showModal) {
        ModalView()
    }
  }
}


struct ModalView: View {

  @EnvironmentObject var viewRouter: ViewRouter

  var body: some View {
    Group {
      Text("Modal view")
      Button(action: {
         self.viewRouter.currentPage = "New View"
      }) {
        Text("Dismiss")
      }
    }
  }
}

Source: Answer by @M Reza Farahani

Here is a solution in Swift: Swift solution

Jonas Deichelmann
  • 3,513
  • 1
  • 30
  • 45

1 Answers1

0

Did not fully test this since I dont have the ViewRouter

You should move the

@Environment(\.presentationMode) var presentationMode

the ModalView and add

self.presentationMode.wrappedValue.dismiss()

to the button action in that ModalView

Edit:

After I added

.animation(.none)

To the ModalView it worked for me

Alright thats one ugly a** comment so putting it here:

    struct ModalView: View {

//  @EnvironmentObject var viewRouter: ViewRouter
    @Environment(\.presentationMode) var presentationMode

  var body: some View {
    Group {
      Text("Modal view")
      Button(action: {
//         self.viewRouter.currentPage = "New View"
        self.presentationMode.wrappedValue.dismiss()

      }) {
        Text("Dismiss")
      }
    }
    .animation(.none)
  }
}
Geart Otten
  • 241
  • 4
  • 11
  • Thank you for your response. But then I still have that transition animation. That'll not fix my problem with the animation. – Jonas Deichelmann Jun 08 '20 at 21:01
  • When i add .animation(.none) to the ModalView it does not have an animation anymore. Hopefully that gives you the desired effect. – Geart Otten Jun 08 '20 at 21:04
  • @Great Otten Where do you add the ``` .animation(.none) ```modifier? I'm not exactly sure how to fix that issue. – Jonas Deichelmann Jun 08 '20 at 21:38
  • Ah my bad. Should have been more clear. Edited my comment above. – Geart Otten Jun 08 '20 at 22:52
  • Unfortunately, dismissing the modal is still animated – Jonas Deichelmann Jun 09 '20 at 09:57
  • 1
    Ah that is weird. I tested it on a simulator and I think it gives the result you want, but when i build it on my real device it does still animate. – Geart Otten Jun 09 '20 at 10:33
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/215573/discussion-between-geart-otten-and-jonas-deichelmann). – Geart Otten Jun 09 '20 at 10:41
  • @GeartOtten I am also unable to dismiss without animation. Can you please guide? – Tul Aug 02 '20 at 06:10
  • @Tul I've tried with the solution above, but it only seems to work on simulators and not real devices. I am not sure how to fix it correctly. – Geart Otten Aug 03 '20 at 07:04
  • This, among other things, showcases feature incompleteness of SwiftUI, and why it pains me that I now have to support an iOS app where the previous developer chose this non-production ready technology. I guess it's ready for some simpler apps, but my client is constantly asking for customizations in areas like this that SwiftUI just can't do yet. – DiggyJohn Jun 27 '22 at 16:08