7

我有一个带有 aa 标签的 Scrollview。为了使标签在顶部中心居中,我对其应用了两个约束:

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[questionLabel]"
                                                             options:0
                                                             metrics:0
                                                               views:viewsDictionary]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.questionLabel
                                                 attribute:NSLayoutAttributeCenterX
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:self
                                                 attribute:NSLayoutAttributeCenterX
                                                multiplier:1.0
                                                  constant:.0]];

如果没有将 questionLabel 的 translatesAutoresizingMaskIntoConstraints 设置为 NO,我会遇到 Core Layout 抛出的异常。它警告我它不能满足以下约束:

(
"<NSLayoutConstraint:0x8e3baf0 V:|-(30)-[UILabel:0xa184ca0]   (Names: '|':WLQuestionnaireSingleQuestionView:0xa184050 )>",
"<NSAutoresizingMaskLayoutConstraint:0x8e3efa0 h=--& v=--& UILabel:0xa184ca0.midY == + 15>"

)

在那里我看到核心布局试图满足一个自动生成的约束,以向后兼容自动调整大小的掩码。据我了解,解决方法应该是将 translatesAutoresizingMaskIntoConstraints 设置为 NO,但不幸的是,这会导致完全黑屏,没有调试消息。

我以编程方式实现了视图,没有笔尖。

4

1 回答 1

0

好吧,我看到这个问题已经很老了。但是,我想对此给出一个答案,因为对其他人来说可能仍然很有趣。当您使用UIScrollViewwith AutoLayout 时,内容大小由约束决定。由于您没有从视图到底部的约束,因此无法正确设置高度。宽度相同,因为您没有为视图指定任何宽度。您刚刚告诉 AutoLayout 它应该位于视图的中心。您的问题可以使用以下代码解决(视图宽度 = 40):

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(30)-[questionLabel]|"
                                                         options:0
                                                         metrics:0
                                                           views:viewsDictionary]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.questionLabel
                                                 attribute:NSLayoutAttributeCenterX
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:self
                                                 attribute:NSLayoutAttributeCenterX
                                                multiplier:1.0
                                                  constant:.0]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[questionLabel(40)]", options: 0, metrics: 0, views: viewsDictionary]];
于 2015-04-11T21:40:10.657 回答