简体   繁体   English

SwiftUI 旋转屏幕使模态不再自动关闭

[英]SwiftUI Rotate screen make modal don’t longer dismiss itself

I have a bug on SwiftUI, when I rotate my device the modal don't longer dismiss, the problem here is that only happen on the device on the simulator works well also on my iPad.我在 SwiftUI 上有一个错误,当我旋转我的设备时,模态不再关闭,这里的问题是只发生在模拟器上的设备上,在我的 iPad 上也能正常工作。

import SwiftUI

struct modalView: View {
    @Environment(\.presentationMode) var presentationMode

    var body: some View {
        Button(action:{
            self.presentationMode.wrappedValue.dismiss()
        }){
            Text("close")
        }
    }
}

struct ContentView: View {
    @State var showModal = false
    var body: some View {
        Button(action: {
            showModal.toggle()
        }){
            Text("modal")
        }
        .sheet(isPresented: self.$showModal, content: {
            modalView()
        })
    }
}

[Bug on my device][1] [我设备上的错误][1]

i have this problem since iOS 13 im currently on iOS 14.2 beta and Xcode 12 GM [1]: https://twitter.com/MisaelLandero/status/1306953785651142656?s=20我有这个问题,因为 iOS 13 im 目前在 iOS 14.2 beta 和 Xcode 12 GM [1]: https://twitter.com/MisaelLandero/status/1306953785651142656?s=2056

Try to use something like this:尝试使用这样的东西:

struct ContentView: View {

  @State private var showModal = false

  // If you are getting the "can only present once" issue, add this here.
  // Fixes the problem, but not sure why; feel free to edit/explain below.
  @Environment(\.presentationMode) var presentationMode


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


struct ModalView: View {

  @Environment(\.presentationMode) private var presentationMode

  var body: some View {
    Group {
      Text("Modal view")
      Button(action: {
         self.presentationMode.wrappedValue.dismiss()
      }) {
        Text("Dismiss")
      }
    }
  }
}

I Found the problem, I was using a condition to show two different navigations views, that break the dismiss modal我发现了问题,我正在使用一个条件来显示两个不同的导航视图,这打破了关闭模式

if self.sizeClass == .compact{
NavigationViewForiPhone()
} else {
NavigationViewForiPad()
} 

looks like thats the problem cuz my view is reload看起来这就是问题所在,因为我的视图正在重新加载

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

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