这让我永远想通了,所以也许有人会觉得这很有帮助:
基本上,仅将按钮包装在 UIViewRepresentable 中是不够的。你必须把它放在一个 ButtonStyle 中,然后用它来设置一个 SwiftUI 按钮的样式。如果您不这样做,您的付款单似乎会损坏!我不确定为什么这是真的,但这是它应该工作的代码:
import SwiftUI
import UIKit
import PassKit
struct PaymentButton: View {
var body: some View {
Button(action: { /* Custom payment code here */ }, label: { EmptyView() } )
.buttonStyle(PaymentButtonStyle())
}
}
struct PaymentButtonStyle: ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
return PaymentButtonHelper()
}
}
struct PaymentButtonHelper: View {
var body: some View {
PaymentButtonRepresentable()
.frame(minWidth: 100, maxWidth: 400)
.frame(height: 60)
.frame(maxWidth: .infinity)
}
}
extension PaymentButtonHelper {
struct PaymentButtonRepresentable: UIViewRepresentable {
var button: PKPaymentButton {
let button = PKPaymentButton(paymentButtonType: .buy, paymentButtonStyle: .black) /*customize here*/
button.cornerRadius = 4.0 /* also customize here */
return button
}
func makeUIView(context: Context) -> PKPaymentButton {
return button
}
func updateUIView(_ uiView: PKPaymentButton, context: Context) { }
}
用法将是:
struct ContentView: View {
var body: some View {
PaymentButton()
}
}