使用自动布局拥有的UILabel,同时也是UITextField彼此相邻 [英] Using Auto Layout to have UILabel and UITextField next to each other

查看:381
本文介绍了使用自动布局拥有的UILabel,同时也是UITextField彼此相邻的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下一个位置一个UILabel实例标签为UITextField实例,文本框像在iOS的设置对话框中完成的。我使用的自动布局和约束与弗洛里安Kuglers FLKAutoLayout扩展的帮助( https://github.com/dkduck/FLKAutoLayout )。

当我设置只对标签和标签和文本框之间的空间约束的领先约束,标签和文本框的宽度调整到他们的内容。 (下图)

但是,当我还设置了一个文本框约束结尾,只有文本框宽度调整其内容,但标签被拉伸。 (上图)

欲周围的行为相反,使得文本框将被拉伸并在标签被调整到它的内容。为什么iOS的决定舒展标签,而不是文本字段的?

  [自addSubview:self.label];
[个体经营addSubview:self.textField];[self.label alignLeadingEdgeWithView:自predicate:@10];
[self.textField constrainLeadingSpaceToView:self.label predicate:@10];//顶部和底部pciture之间差
// [self.textField alignTrailingEdgeWithView:自predicate:@ - 25];


解决方案

编辑:所以拉胡尔的评论后,我用宽度和优先播放。添加以下宽度约束领先进入有点错误的方向,因为在进入一些很长的值,标签是调整太小了。

  [self.label constrainWidth:@0 @ 1];

所以,我终于在的COM pression阻力和内容抱死(读了<一个href=\"http://www.objc.io/issue-3/advanced-auto-layout-toolbox.html\">http://www.objc.io/issue-3/advanced-auto-layout-toolbox.html)解决的办法是对标签的卧式内容抱死优先级设置为较高的值。

  [self.label setContentHuggingPriority:500 forAxis:UILayoutConstraintAxisHorizo​​ntal];

然后,即使进入大值将让标签逗留宽度为内容!

解决方案不加入优先宽度的限制,但组COM pression阻力和内容抱死属性的UIViews的内在含量的大小。

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"];

解决方案

EDITED: So after the comment of Rahul I played with width and priorities. Adding the following width constraint lead into the somewhat wrong direction, because of entering some very long value, the label was resized too small.

[self.label constrainWidth:@"0@1"];

So I finally read up on "Compression Resistance and Content Hugging" (http://www.objc.io/issue-3/advanced-auto-layout-toolbox.html) and the solution is to set the Horizontal Content Hugging priority of the label to a higher value.

[self.label setContentHuggingPriority:500 forAxis:UILayoutConstraintAxisHorizontal];

Then even entering big values will let the label stay as width as its content!

Solution is not adding a width constraint with priority, but to set Compression Resistance and Content Hugging properties for the intrinsic content size of the UIViews.

这篇关于使用自动布局拥有的UILabel,同时也是UITextField彼此相邻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆