Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autolayout resize button based on text and have textfield fill available space

I am working on an ios application using autolayout,

I have a UITextField and a UIButton next to it horizontally.

The UIButton text is changed dynamically. It can be very short (3 letters) or very long (15 letters)

I need to add constraint to achieve the following:

1) when I change the UIbutton text, I need the button to resize to fit the new text

2) the UITextField should resize to take the available space left after the button text change

I tried to do the following (like shown in the picture):

  • Added a leading constraint for the UITextView to the superview 20

  • Added a trailing constraint for the UIButton to the superview 20

  • Added a fixed distant between them 10

enter image description here

but now I'm stuck on how to make the UIButton resize with the text and the textfield to take the available size.

Any help is appreciated

Thank you

like image 393
Y2theZ Avatar asked Apr 22 '14 17:04

Y2theZ


1 Answers

Try these autolayout constraints:

23226220_Autolayout_Constraints


Basically:

  1. UITextField object's width constraint should be a Less Than or Equal relation
  2. UIButton object's width constraint should be a Greater Than or Equal relation
  3. UITextField object's horizontal Content Hugging Priority should be 249 (as per my constraint settings shown above)

And related code as:

[buttonObject setTitle:@"Button title is too... zzz" forState:UIControlStateNormal];
[buttonObject sizeToFit];

...or something along these lines

like image 136
staticVoidMan Avatar answered Nov 15 '22 18:11

staticVoidMan