Autolayout - intrinsic size of UIButton does not include title insets

Ben Packard picture Ben Packard · Jul 23, 2013 · Viewed 72.8k times · Source

If I have a UIButton arranged using autolayout, its size adjusts nicely to fit its content.

If I set an image as button.image, the instrinsic size again seems to account for this.

However, if I tweak the titleEdgeInsets of the button, the layout does not account for this and instead truncates the button title.

How can I ensure that the intrinsic width of the button accounts for the inset?

enter image description here

Edit:

I am using the following:

[self.backButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];

The goal is to add some separation between the image and the text.

Answer

n.Drake picture n.Drake · Feb 2, 2015

You can get this to work in Interface Builder (without writing any code), by using a combination of negative and positive Title and Content Insets.

enter image description here

Update: Xcode 7 has a bug where you cannot enter negative values in the Right Inset field, but you can use the stepper control next to it to decrease the value. (Thanks Stuart)