SwiftUI 全屏透明按钮

Ser*_* Di 5 ios swift swiftui

我尝试添加全屏透明按钮:

Button(action: {
       // my action
}) {
    Rectangle()
     .opacity(0)
}
Run Code Online (Sandbox Code Playgroud)

但如果.opacity()小于 0.1 按钮动作停止工作。如何实现全屏透明按钮?

在 iOS 14.3(Sim)、iOS 14.2(iPhone X)、Xcode 12.3 上测试

Asp*_*eri 5

这是可能的解决方案。使用 Xcode 12.1 / iOS 14.1 进行测试

struct DemoClearButton: View {
    var body: some View {
        Color.clear
            .contentShape(Rectangle())
            .onTapGesture {
                print(">> transparent tapped")
            }
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:可能代替您想要添加的用法.edgesIgnoringSafeArea(.all)