简体   繁体   中英

SwiftUI full screen transparent button

I try to add full screen transparent button:

Button(action: {
       // my action
}) {
    Rectangle()
     .opacity(0)
}

But in case .opacity() is less than 0.1 button action stop working. How to implement full screen transparent button?

Tested on iOS 14.3(Sim), iOS 14.2(iPhone X), Xcode 12.3

Here is possible solution. Tested with Xcode 12.1 / iOS 14.1

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

Note: probably in place of usage you'd wanted to add .edgesIgnoringSafeArea(.all)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM