简体   繁体   中英

onTapGesture not working with padding modifier in SwiftUI

For some reason, the onTapGesture modifier doesn't work when I add a padding modifier. If you look below, you'll see the 3 places where I've tried to add the padding modifier. The onTapGesture modifier works fine if I comment out/remove the padding modifier, but if i uncomment any of those paddings, the onTapGesture stops working. I don't know what's going on here.

if noninitialRun == false {
                ZStack(alignment: .topTrailing) {
                    Walkthrough()
                        //.padding(-16)
                    
                    VStack {
                        Image(systemName: "xmark")
                            //.padding()
                            .onTapGesture {
                                print("tap")
                                noninitialRun = true
                                UserDefaults().setValue(true, forKey: "notFirstRun")
                        }
                        //.padding()
                    }
                }
            }

It works fine with Xcode 12.3. I think your Xcode version caused it. But nevertheless, you can try: .contentShape(Rectangle()) .


VStack {
  Image(systemName: "xmark")
  .padding()
  .onTapGesture {
    print("tap")
    noninitialRun = true
    UserDefaults().setValue(true, forKey: "notFirstRun")
  }
  .padding()
}.contentShape(Rectangle())

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