简体   繁体   English

有没有办法在 SwiftUI 中关闭没有 animation 的模态视图?

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

Is there a way to dismiss a modal view without animation in SwiftUI?有没有办法在 SwiftUI 中关闭没有 animation 的模态视图?

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.我想在不关闭 animation 的情况下关闭模态,因为我想使用视图路由器从模态视图导航到新的 SwiftUI 视图。 Everything is working, except for the transition animation from the modal view to the new full-screen view.一切正常,除了 animation 从模态视图到新的全屏视图的转换。 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资料来源: @M Reza Farahani 的回答

Here is a solution in Swift: Swift solution这是Swift中的一个解决方案: Swift解决方案

Did not fully test this since I dont have the ViewRouter因为我没有 ViewRouter,所以没有完全测试这个

You should move the你应该移动

@Environment(\.presentationMode) var presentationMode

the ModalView and add模态视图并添加

self.presentationMode.wrappedValue.dismiss()

to the button action in that ModalView到该 ModalView 中的按钮操作

Edit:编辑:

After I added我添加后

.animation(.none)

To the ModalView it worked for me对于 ModalView 它对我有用

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 did you fixed dismiss a modal view without animation @Geart Otten 你修复了没有 animation 的模式视图吗

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM