【问题标题】:SwiftUI modals in Xcode Beta 6?Xcode Beta 6 中的 SwiftUI 模式?
【发布时间】:2019-08-23 08:32:25
【问题描述】:

以前在 SwiftUI(Xcode Beta 5)中,模态的工作方式如下:

struct ContentView: View {

    @State var modalIsPresented: Bool = false

    var body: some View {

        Button(action: {

            self.modalIsPresented = true

        }) {

            Text("Show modal")

        }

        .sheet(isPresented: $modalIsPresented, content: {

            ModalView()

        })

    }

}

struct ModalView: View {

    @Environment(\.presentationMode) var presentationMode

    var body: some View {

        Button(action: {

            self.presentationMode.value.dismiss()

        }) {

            Text("Hide modal")

        }

    }

}

但现在在 Xcode Beta 6 中,我找不到关闭模式的方法。 presentationModevalue 属性已经没有了,其他属性似乎也没有什么有用的方法可以使用。

如何在 Xcode Beta 6 中关闭 SwiftUI 模式?

【问题讨论】:

  • 您是否尝试过传入 $modalIsPresented 并将其设置为 false?
  • 谢谢@Fabian - 你能把它作为答案发布吗?

标签: ios swift xcode swiftui xcode11


【解决方案1】:

使用 wrappedValue 而不是 value 似乎在 Xcode Beta 6 中有效:

self.presentationMode.wrappedValue.dismiss()

【讨论】:

    【解决方案2】:

    你可以关闭.sheet.popover.actionSheet,方法是传入控制其显示的绑定,此处为$modalIsPresented,并将其设置为 false 以编程方式关闭它。

    【讨论】:

      【解决方案3】:

      试着检查一下:

      .presentation(showModal ? Modal(Text("Modal screen"), onDismiss: {
      self.showModal.toggle()
      }) : nil)
      

      默认的模态显示并不能证明用户关闭模态的任何视觉方式,但从 iOS 13 开始,用户可以向下滑动视图以使其消失。

      详情:https://alejandromp.com/blog/2019/06/24/improving-swiftui-modal-presentation-api/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-09-05
        • 2014-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-24
        • 2014-10-07
        相关资源
        最近更新 更多