How can I increase the Tap Area for UIButton?

Sven Bauer picture Sven Bauer · Jun 25, 2015 · Viewed 39.4k times · Source

I use UIButton with auto layout. When images are small the tap area is also small. I could imagine several approaches to fix this:

  1. increase the image size, i.e., place a transparent area around the image. This is not good because when you position the image you have to keep the extra transparent border in mind.
  2. use CGRectInset and increase the size. This does not work well with auto layout because using auto layout it will fall back to the original image size.

Beside the two approaches above is there a better solution to increase the tap area of a UIButton?

Answer

Travis picture Travis · Jun 25, 2015

You can simply adjust the content inset of the button to get your desired size. In code, it will look like this:

button.contentEdgeInsets = UIEdgeInsets(top: 12, left: 16, bottom: 12, right: 16)
//Or if you specifically want to adjust around the image, instead use button.imageEdgeInsets

In interface builder, it will look like this:

interface builder