I am trying to position a UILabel instance label next to a UITextField instance textfield like done in the iOS Settings dialogs. I'm using Auto Layout and constraints with the help of Florian Kuglers FLKAutoLayout extensions (https://github.com/dkduck/FLKAutoLayout).
When I set only a leading constraint for the label and a space constraint between label and textfield, the label and textfield widths are adjusted to their content. (bottom picture)
But when I set also a trailing constraint for the textfield, only textfields width is adjusted to its content, but label is stretched. (top picture)
I want to behave the opposite around, so that the textfield will be stretched and the label is adjusted to its content. Why does iOS decide to stretch the label instead of the textfield?
[self addSubview:self.label];
[self addSubview:self.textField];
[self.label alignLeadingEdgeWithView:self predicate:@"10"];
[self.textField constrainLeadingSpaceToView:self.label predicate:@"10"];
// difference between top and bottom pciture
// [self.textField alignTrailingEdgeWithView:self predicate:@"-25"];
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…