Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly create a UIButton that resizes to the text length in Xcode 4?

In Xcode 4 you should be able to design UI interfaces that do resize based on their content. Still it looks that the are some missing pieces regarding how to obtain this.

I have a button that is configured in the XIB file to resize to the right and it has plenty of space to do that.

The problem is that it doesn't seam to resize the button when the label is updated.

This can be fixed by adding a [button sizeToFit]; after the label is changed.

Still I am looking for a solution that works without adding this manual step into the code.

button-ios-43

button-ios-50b7

I mention that there is enough space to expand the button further, still sizeToFit seams to to enlarge the button but still not enough to fit the entire text.

What am I missing?

like image 338
sorin Avatar asked Dec 01 '22 07:12

sorin


1 Answers

[button sizeToFit] didn't work for me, but I've found a way using IB alone (xcode 4.5):

  1. Click on the UIButton
  2. in the Size inspector drag content hugging to 1 (both horizontal and vertical)
  3. drag compression resistance to 999 (for both)
  4. under the UIButton's constraints click on Width and change priority to 250
  5. Do the same for Height
  6. You can use the UIButton's inset to control padding for left/right/top/bottom
like image 56
marmor Avatar answered Feb 16 '23 05:02

marmor