c0nman
c0nman

Reputation: 309

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()
                    }
                }
            }

Upvotes: 1

Views: 3292

Answers (1)

batuhankrbb
batuhankrbb

Reputation: 706

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())

Upvotes: 8

Related Questions