Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I increase the Tap Area for UIButton?

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?

like image 359
Sven Bauer Avatar asked Jun 25 '15 17:06

Sven Bauer


People also ask

What is a UIButton?

A control that executes your custom code in response to user interactions.

How do I change text on UIButton?

Programmatically. To make a multi-line text in UIButton, you insert a new line character ( \n ) wherever you want in button title and set lineBreakMode to byWordWrapping . You can adjust text alignment with . textAlignment .


1 Answers

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

like image 81
Travis Avatar answered Oct 07 '22 05:10

Travis