Anthony Frizalone Anthony Frizalone - 5 months ago 79
iOS Question

How can I make UIButton respond to touch on the transparent areas of a PNG image?

UIButton ignores touch if you touch it where there's no image alpha. Can I change it to respond to touch on its entire bounds?

Answer

UIButton ignores touch if you touch it where there's no image alpha. Can I change it to respond to touch on its entire bounds?

One easy way that I think still works is to set the background color to something that's only mostly transparent. An alpha that's very small but still greater than 0.1 should look transparent but still respond to touches.

Otherwise, yes, you can override -hitTest:withEvent: such that it returns YES even if the touched area is transparent.

Comments