我有一个按钮,我想在它上面放一个半透明的渐变叠加。
Button(action: {
print("Pressed")
}) {
Text("Press me")
}
.overlay(
LinearGradient(
gradient: Gradient(colors: [.clear, Color.black.opacity(0.3)]),
startPoint: .top,
endPoint: .bottom
).disabled(true)
)
Run Code Online (Sandbox Code Playgroud)
即使渐变有disabled(true)它仍然吃触摸并且不会将它们转发到实际按钮。.allowsHitTesting(false)提供相同的结果。
知道有什么问题吗?
注意:我知道我可以将覆盖层放置到Text("Press me")但我不想要它。(这只是展示问题的示例代码)
编辑:此问题已在 Xcode 11.2 中解决?
以下代码适用于 Xcode 11.2 / iOS 13.2
struct TestButtonWithOverlay: View {
var body: some View {
Button(action: {
print("Pressed")
}) {
Text("Press me")
.padding()
}
.overlay(
LinearGradient(
gradient: Gradient(colors: [.clear, Color.black.opacity(0.3)]),
startPoint: .top,
endPoint: .bottom
)
.allowsHitTesting(false) // !!! must be exactly here
)
}
}
struct TestButtonWithOverlay_Previews: PreviewProvider {
static var previews: some View {
TestButtonWithOverlay()
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3145 次 |
| 最近记录: |