3

I have a set of UIButtons where each label is just a number. On each button of them, I set the background color to clear, there is no UIImage set.

The problem is those buttons are only tappable on their label which makes it hard to tap.

I checked around for this problem and some solutions were to:

  • change the content insets: button.contentEdgeInsets = UIEdgeInsetsMake(-50, -50, -50, -50) -> didn't change a thing
  • subclass UIButton and override pointInside -> didn't work, pointInside was never reached when tapping on the button but outside the label.

I'm just surprised there is no easy solution for such a normal use case. I need the background clear as I am using those buttons for today's extension.

Any idea how I could do it?

EDIT:

Here is a sample project where I reproduced the problem.

It seems it's linked to static cell in a today extension as it's okay with static cell in a view controller within the app.

Nico
  • 6,269
  • 9
  • 45
  • 85
  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself." - please add code and possibly a sample project to your question – Aaron Brager Oct 10 '15 at 00:08

1 Answers1

14

Apply a backgroundColor of UIColor(white:0.000, alpha:0.020). The human eye cannot see this color — it appears completely transparent in the Today widget — but the button background is now tappable.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Perfect. Thanks a lot. Do you know why it behaves like this with Today widget? – Nico Oct 12 '15 at 03:44
  • 1
    We are not in charge of how hit-testing works in the Today environment, so it probably has to do with that. Presumably the button background fails the hit-test if it is clear, just as would be true of a plain UIView. So I solved it in the same way I would if this were a plain UIView. I suppose we could figure out the details if we really had to. Personally I prefer to solve the problem and walk away. :) – matt Oct 12 '15 at 14:48