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

2

8

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

Bryon answered 8/6, 2020 at 18:27 Comment(1)
did you manage to fix this? I also cant dismiss the modal without the default animationTiter
G
1

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)
  }
}
Gopherwood answered 8/6, 2020 at 20:59 Comment(10)
Thank you for your response. But then I still have that transition animation. That'll not fix my problem with the animation.Bryon
When i add .animation(.none) to the ModalView it does not have an animation anymore. Hopefully that gives you the desired effect.Gopherwood
@Great Otten Where do you add the ``` .animation(.none) ```modifier? I'm not exactly sure how to fix that issue.Bryon
Ah my bad. Should have been more clear. Edited my comment above.Gopherwood
Unfortunately, dismissing the modal is still animatedBryon
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.Gopherwood
Let us continue this discussion in chat.Gopherwood
@GeartOtten I am also unable to dismiss without animation. Can you please guide?Apocopate
@Apocopate 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.Gopherwood
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.Sapotaceous
C
0

You can use the normal DismissAction provided in the environment and withAnimation as documented here: https://developer.apple.com/documentation/swiftui/managing-user-interface-state#Animate-state-transitions

struct example: View {
    @Environment(\.dismiss) private var dismiss
    var body: some View {
        Button {
            withAnimation(nil) {
                dismiss()
            }
        } label: {
            Text("dismiss")
        }
    }
}
Cabdriver answered 1/7 at 16:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.