Our UITextField instances are using the "default" settings. They auto expand based on user input. We'd like to set them to a larger initial width so they don't have to increase their width. What is the correct way to do this to while maintaining an 'adaptive' layout? Or is this just contrary to iOS best practices and we should leave it as is?
-
default textfield have dynamic width and can be set adaptive with storyboard, what's the actual problem? – Tj3n Nov 09 '16 at 03:32
-
The standard text the users enter into these text fields is about 25% larger than the initial text field size. So I'd like to increase the initial width do users don't have to see the effect of the text field widening. – Marcus Leon Nov 09 '16 at 03:34
-
You mean height? the width can be set at any value initialy isnt it – Tj3n Nov 09 '16 at 03:48
-
If you want a text field to have a certain width, give it a width constraint or leading/trailing constraints. I don't get what the problem is. – matt Nov 09 '16 at 03:48
-
Ill look at that. I wasn't aware you could use a constraint for this purpose. – Marcus Leon Nov 09 '16 at 04:06
-
He want to set minimum width for the textField and save possibility of automatic growing of the textField – Andrew Romanov Nov 09 '16 at 04:10
1 Answers
To determine a view's size the autolayout uses two methods:
- (CGSize)intrisicContentSize //with
- (UILayoutPriority)contentCompressionResistancePriorityForAxis:(UILayoutConstraintAxis)axis;
- (UILayoutPriority)contentHuggingPriorityForAxis:(UILayoutConstraintAxis)axis;
and
width and height constrtaints (size constraints)
//also the autolayot can use inner constraints, but here it is not important
If you have size constraints and intrisic content size with different values, you shout set priority for constraints and for intrisicContentSize.
In your case, you can set low priority for contentHuggingPriorityForAxis for horizontal axis. Set middle priority for width constraint. Set Hight priority for contentCompressionResistancePriorityForAxis for the horizontal axis. After that, if text be smaller then width constraint, the textField will have width same to constant of the width constraint. Then text will have more width then constraint, the textField will grow out of the width constraint. Also I recommend for you add constraint with "<=" relation for max width and required priority

- 4,774
- 3
- 25
- 40