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?

sorin
  • 161,544
  • 178
  • 535
  • 806

3 Answers3

22

[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
marmor
  • 27,641
  • 11
  • 107
  • 150
  • 2
    This should be the accepted answer in the iOS 7 era. – Keller Apr 10 '14 at 17:47
  • setting compression resistance to 1000 is bad practice. set it to 999 instead: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/ConflictingLayouts.html#//apple_ref/doc/uid/TP40010853-CH19-SW1 – Damnum Apr 20 '16 at 05:59
  • @Damnum i don't think 999 is some magic value, they just give it as an example of a high priority number, I think 1000 is just the same. – marmor Apr 20 '16 at 06:44
  • 1
    1000 means "required", so the custom constraint might create a conflict with a constraint created by the system. So any number from 751-999 should do it – Damnum Apr 20 '16 at 08:58
2

You need to call sizeToFit on the button after setting it's title to make it resize to fit the given title.

You can remove the manual step by subclassing UIButton, overriding the setTitle:forControlState: method and implementing the call to sizeToFit in there. Don't forget to call the super implementation, otherwise your title won't be set and bad things will happen.

Jasarien
  • 58,279
  • 31
  • 157
  • 188
  • sizeToFit is supposed to work but it seams that it does not enlarge the button enough. – sorin Sep 05 '11 at 14:36
  • Has this button been subclassed at all? Are you using a custom font for the label? – Jasarien Sep 05 '11 at 16:50
  • 1
    @Jasarien same problem here. I'm not subclassing but using a custom font and the label isn't expanding to fit contents on a call to sizeToFit but it contracts when necessary! Wonder if there is some problems with the constraints. No idea how to go about fixing them. – trss Jul 24 '12 at 18:01
-4

In XCode 4.5 and above, this can now be done by using 'Auto-layouting / Constraints'.

Major advantages are that:

  1. You don't need to programatically set frames at all!
  2. If done right, you don't need to bother about resetting frames for orientation changes.
  3. Also, device changes needn't bother you (read, no need to code separately for different screen sizes).

A few disadvantages:

  1. Not backward compatible - works only for iOS 6 and above.
  2. Need to get familiarised (but will save time later on).

Coolest thing is we get to focus on declaring an intent such as:

  • I want these two buttons to be of the same width; or
  • I need this view to be vertically centered and extend to a max entent of 10 pts from the superview's edge; or even,
  • I want this button/label to resize according to the label it is displaying!

Here is a simple tutorial to get introduced to auto-layouting: http://www.raywenderlich.com/20881/beginning-auto-layout-part-1-of-2

For a more detailed look at things, go to: http://www.techotopia.com/index.php/An_Introduction_to_Auto_Layout_in_iOS_6

It takes some time at first, but it sure looks like it will be well worth the effort.

Cheers!

codeburn
  • 1,994
  • 17
  • 20