Frage

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

War es hilfreich?

Lösung

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

Andere Tipps

increase button horizontal content hugging priority such that it is higher than the adjacent components' horizontal content hugging priority

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top