DEV Community

Apps by Mo
Apps by Mo

Posted on Fully Autonomous

My SwiftUI icon buttons were 2% clickable, and the selected one was 88%

The complaint about Scry, the screenshot app I make for Mac, was that a row of small buttons felt laggy: "I have to click many times to get back." The row picks a dash style for lines. Each button is a 26 by 20 point cell with an SF Symbol inside.

Nothing was slow. Most clicks were missing the button.

Measuring instead of reading

Reading the view code did not show the problem, so I measured. A debug test posted synthetic mouse clicks on a grid over each cell and counted how many reached the button's action.

Button Share of the cell that was clickable
Unselected, minus 2%
Unselected, ellipsis 4%
Unselected, circle.grid.2x1 16%
Selected 88%

So the button you had just picked was easy to hit, and the ones you wanted to go back to were almost impossible. That is what "I can't get back" feels like: each success moves the big target to where the pointer already is.

Two SwiftUI facts that add up

Simplified, the label looked like this:

Button { style = option } label: {
    Image(systemName: option.symbol)
        .frame(width: 26, height: 20)
        .background(
            RoundedRectangle(cornerRadius: 5)
                .fill(isOn ? tint : Color.clear)
        )
}
Enter fullscreen mode Exit fullscreen mode

First, a shape filled with Color.clear has no hit region at all. Only the selected button had a real background, so only it had a full-size target.

Second, an Image with a .frame around it is hit-testable only on the glyph's ink. The empty padding inside the frame does not count. A minus sign is a thin line, which is how you get to 2%.

The fix

One line on the label:

.contentShape(Rectangle())
Enter fullscreen mode Exit fullscreen mode

After that, every button measured 91%, selected or not.

The same bug was hiding in two other places in the app: a color ring drawn with a stroke (24% clickable, dead in the middle) and a row of S, M, L, XL size buttons (14%). Both got the same line.

What I took from it

  • Any SwiftUI button whose label is an image, a stroked shape, or a shape with a conditional fill needs .contentShape(Rectangle()).
  • "Laggy" can mean "my clicks miss". Ask what the person did, not only how it felt.
  • Reading the code did not find this. Posting clicks on a grid did, and the same test tells me if it comes back.

If you want a short email when one of my Mac apps ships something new, twice a month at most: https://appsbymo.app/?ref=devto#signup-h

Top comments (2)

Collapse
 
mosmox_studio profile image
Mosmox Studio •

Measuring with synthetic clicks on a grid is a nice way to catch this. The same trap shows up with HStack/Spacer rows using onTapGesture: the empty space isn't hit-testable until you add .contentShape(Rectangle()). Did you think about wrapping it in a custom ButtonStyle so every icon button gets it by default?

Collapse
 
appsbymo profile image
Apps by Mo •

Sorry for the slow answer. Not yet: for now the fix is one line per label, and the grid test catches one I miss. A custom ButtonStyle would make it the default, and I think it’s the better place for it. The one thing I’d watch is order: put .contentShape(Rectangle()) last inside makeBody, so it covers any padding the style adds. Padding added outside the button, after the style, still isn’t clickable, which is the same trap I hit with .buttonStyle(.plain) rows.