【问题标题】:Black screen after setting translatesAutoresizingMaskIntoConstraints to NO设置 translatesAutoresizingMaskIntoConstraints 为 NO 后黑屏
【发布时间】:2013-06-11 02:20:38
【问题描述】:

我有一个带有标签的 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,但不幸的是,这会导致完全黑屏,没有调试消息。

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

【问题讨论】:

  • 我正在与相同类型的行为作斗争。你找到解决方法了吗?
  • 我也遇到了同样的问题。
  • 很抱歉,时间太长了,我无法再访问源代码了。我相信有一种解决方法可以作为我的问题的非常具体的解决方案(可能首先避免这个问题),但我不能说我现在有这个具体问题的解决方案。
  • 您有示例项目吗?您是否尝试更改标签背景颜色以查看它在哪里?你的屏幕怎么黑的?根视图的背景颜色是什么?

标签: ios cocoa-touch nslayoutconstraint


【解决方案1】:

嗯,我看到这个问题已经很老了。但是,我想对此给出一个答案,因为对其他人来说可能仍然很有趣。当您使用带有 AutoLayout 的 UIScrollView 时,内容大小由约束决定。由于您没有从视图到底部的约束,因此无法正确设置高度。宽度相同,因为您没有为视图指定任何宽度。您刚刚告诉 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]];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多