我已经访问过这个答案和 the present
method proposed here不起作用(不再?)。当我使用 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
}
}
最佳答案
我不是 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())
}
}
请注意,您仍然需要通过委托(delegate)在外部点击来连接取消按钮以及常规关闭,否则带有按钮的底层 UI 不会再次响应触摸。
关于swiftui - 如何在仅 SwiftUI 的项目中正确呈现 PKPaymentAuthorizationViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62638416/