关于您的评论:出现这种情况是因为滚动视图的大小取决于其内容(请参阅部分Pure Auto Layout Approach)。所以,标签的大小不能取决于滚动视图的大小。
有一个解决方法这一点 - 你可以设置标签和主视图之间,例如限制:
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.label
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.view
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:40]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.label
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationLessThanOrEqual
toItem:self.view
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:-40]];
采用这种方法就会有距离的至少40点左右之间(右)一侧和滚动视图的左侧(右侧)(因为滚动视图将填充主视图)。
或者,您可以在标签和主视图的width
之间设置一个约束。它会是这样的:
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.label
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationLessThanOrEqual
toItem:self.view
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:-40]];
所以现在标签的宽度不会比主视图的width - 40
大。
您是否已经观看过WWDC 2012和2013中所有与自动布局相关的会话? –
我现在已经做了,我找到了一种方法来处理它,但代价是在标签上设置了一个固定的宽度。这不是我想要的,但它完成了工作 – greg