如何在仅SwiftUI项目中正确显示PKPaymentAuthorizationViewController?

时间:2020-06-29 12:41:51

标签: swiftui uikit applepay passkit

我已经访问过此答案,the present method proposed here不起作用(不再吗?)。当我使用UIViewControllerRepresentable并将其显示为工作表时,它看起来很糟糕:

Screenshot 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
    }
}

1 个答案:

答案 0 :(得分: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())
            }
        }

请注意,您仍然需要通过在代表处轻按外部来挂接“取消”按钮以及常规关闭操作,否则带有按钮的基础UI不会再次响应触摸。