如何在仅 SwiftUI 的项目中正确呈现 PKPaymentAuthorizationViewController?

Dep*_*o B 5 uikit passkit applepay swiftui

我已经访问过这个答案,这里提出present方法不起作用(不再?)。当我使用 aUIViewControllerRepresentable然后将其显示为工作表时,它看起来非常糟糕:

屏幕截图 UIViewControllerRepresentable 表

如果我使用overlay它,它看起来就像我想要的那样,但overlay不能从Button.

这是(精简的)代码:

public struct ContentView: View {
    @ObservedObject var model: RootViewModel
    @State private var tappedDonate = false

    public var body: some View {
            Button(action: {
                tappedDonate = true
            }, label: {
                Text("Donate")
                    .frame(width: 300, height: 44, alignment: .center)
            })
            .frame(width: 300, height: 20, alignment: .center)
            .padding()
            .background(Color.black)
            .foregroundColor(.white)
            .cornerRadius(22)
            .sheet(isPresented: $tappedDonate) {
                ApplePayWrapper(request: model.buildApplePayment())
                    .background(Color.clear)
            }
    }

    public init(model: RootViewModel) {
        self.model = model
    }
}
Run Code Online (Sandbox Code Playgroud)

Dep*_*o B 4

我并不是 100% 相信这是最好的方法,但我设法让它在视觉上工作,就像我希望它使用以下方式工作一样ZStack


        ZStack {           
            Button(action: {
                tappedDonate = true
            }, label: {
                Text("Donate")
                    .frame(width: 300, height: 44, alignment: .center)
            })
            .frame(width: 300, height: 20, alignment: .center)
            .padding()
            .background(Color.black)
            .foregroundColor(.white)
            .cornerRadius(22)
            if tappedDonate {
                ApplePayWrapper(request: model.buildApplePayment())
            }
        }
Run Code Online (Sandbox Code Playgroud)

请注意,您仍然需要通过委托点击外部来连接取消按钮以及常规关闭,否则带有按钮的底层 UI 不会再次响应触摸。