Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton Padding in Swift 3

So, I spent two days trying to figure out how to add padding around UIButton in Swift 3. I set a custom image (not background image) and trying to increase tappable area with the following code:

backButton.imageEdgeInsets = UIEdgeInsets(top: -44, left: -44, bottom: -44, right: -44)

But it doesn't work! My biggest success is that the button itself becomes bigger with tappable area staying ridiculously small. Please, help.enter image description here

like image 897
Andriyas Redel Avatar asked Nov 15 '16 21:11

Andriyas Redel


1 Answers

You can increase the tappable area with content insets. The button frame increases as well, but maintains your content size while creating an invisibly larger tappable area.

By setting the left inset to 0 you can make the image appear all the way to the left, if you're trying to get 'log in' to appear closer to your arrow, but I'm not sure if that's what you're trying to accomplish.

enter image description here

Or programmatically:

button.contentEdgeInsets = UIEdgeInsets(top: 44, left: 0, bottom: 44, right: 44)

You can also add your image and title together in one button, then set the images left inset to 0 and the titles left inset to 20 for example, and create the effect you're looking for. Again, assuming.

enter image description here

like image 154
Frankie Avatar answered Oct 09 '22 02:10

Frankie