How to change default UIButton padding when using auto-layout
Asked Answered
P

2

18

When using auto-layout in iOS 6, a UIButton's intrinsic content size appears to include about 10px of padding around the button text. Is there any way to control this padding value? For example, I'd like to be able to do something like this, which would set the padding to 5px on each side:

[button setPadding:UIEdgeInsetsMake(5, 5, 5, 5)];

I have tried setting contentEdgeInsets, titleEdgeInsets, and imageEdgeInsets, all to no avail. Oddly, specifying a negative left or right value in contentEdgeInsets appears to have some effect, but not top or bottom.

Either way, I'd like to be able to specify the actual padding value as a positive number, not an adjustment to the default expressed as a negative number. Anyone know if this is possible?

Padauk answered 12/6, 2013 at 18:44 Comment(0)
P
13

I realize that I posted this question over two years ago, but I wanted to follow up and note that the contentEdgeInsets property now appears to work correctly with autolayout. For example, the following code produces a button with a 20-pixel margin around its content:

button.contentEdgeInsets = UIEdgeInsetsMake(20, 20, 20, 20);
Padauk answered 30/9, 2015 at 13:39 Comment(1)
To anyone reading this, note that you have to set CONTENT insets, NOT TITLE insets.Washtub
F
4

I'm not sure if you can do this without subclassing UIButton. If you are willing to subclass, overriding this method might do the trick (I have not tried this):

- (UIEdgeInsets)alignmentRectInsets { return UIEdgeInsetsMake(5, 5, 5, 5); }

Frontogenesis answered 29/6, 2013 at 5:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.