设置后将黑色屏幕翻译为"无限制"的"任务大小"

ff1*_*f10 7 cocoa-touch ios nslayoutconstraint

我有一个带有标签的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]];
Run Code Online (Sandbox Code Playgroud)

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

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

)

在那里,我看到Core Layout试图满足一个约束,该约束已经自动生成为与自动调整掩码向后兼容.在我的理解中,对此的修复应该是将translatesAutoresizingMaskIntoConstraints设置为NO,但不幸的是,这导致完全黑屏,没有调试消息.

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

bor*_*ero 0

嗯,我发现这个问题已经很老了。然而我想给出一个答案,因为它可能对其他人来说仍然很有趣。当您使用UIScrollViewAutoLayout 时,内容大小由约束决定。由于从视图到底部没有约束,因此无法正确设置高度。宽度也是如此,因为您没有为视图指定任何宽度。您刚刚告诉 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]];
Run Code Online (Sandbox Code Playgroud)