TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Make entire contents of button tappable without adding padding?

Forums > SwiftUI

I've got a bunch of buttons within a list. The buttons have text on one side, a spacer, and then text on the other side. I would like for the entire row to be tappable, but I don't want to add any padding to the row. Reference below:

Reference

I've tried the following:

Button(...) { ... }
    .contentShape(Rectangle())

but that doesn't seem to work.

3      

Have you tried it with a .frame modifier? I use this for SFSymbols and images when I want to be the tapable area greater than the image.

3      

I figured it out! The solution was to put the .contentShape inside the button's label parameter, not to apply it to the button itself.

Button {
  // action
} label: {
  label
    .contentShape(Rectangle())
}

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.