我已经访问过此答案,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
}
}
答案 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不会再次响应触摸。