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

How to properly present PKPaymentAuthorizationViewController in a SwiftUI only project?

我已经访问过这个答案,但 不起作用(不再?)。当我使用 UIViewControllerRepresentable 然后将其显示为 sheet 时,它看起来非常糟糕:

如果我使用 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
    }
}

我不是 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())
            }
        }

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