【问题标题】:Constraint must contain a first layout item NSLayoutConstraint约束必须包含第一个布局项 NSLayoutConstraint
【发布时间】:2012-09-17 02:39:25
【问题描述】:

我的 LayoutConstraints 工作正常,然后突然间,在我的视图中添加约束时我开始得到这个。

    *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: Constraint must contain a first layout item'
*** First throw call stack:
(0x7cd012 0x2017e7e 0x7ccdeb 0xde6bf1 0x9c487 0x994a3 0x414fb 0x20224 0x5c6c0 0xc30e83 0x78c376 0x78be06 0x773a82 0x772f44 0x772e1b 0x28eb7e3 0x28eb668 0x149465c 0x2cfd 0x2c25)
libc++abi.dylib: terminate called throwing an exception

互联网上或 Apple Docs 上没有关于错误消息含义的即时文档。

只是想知道是否有人遇到过这个错误,并且知道我可以做些什么来解决它?

【问题讨论】:

    标签: ios autolayout nslayoutconstraint


    【解决方案1】:

    此错误/崩溃的另一个原因可能是,传递给 [NSLayoutContraint constraintWithItem ...] 方法的视图为零。

    【讨论】:

    • @smileBot 我设置为强但返回零。我已经扩展了 UIView 和 loadNib 文件并更新了约束,但由于 self.subView 为 nil 而崩溃。 [self addConstraint:[NSLayoutConstraint constraintWithItem:self.subView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
    【解决方案2】:

    啊,找到问题了。 对于遇到此问题的任何人,这是因为尚未在 nib/UIView 中创建视图,因此无法应用任何约束。

    我将约束代码从 initWithNib 方法移到 viewDidLoad 并且错误自然停止发生。

    【讨论】:

    • 即使在 viewDidLoad 中创建/添加了约束,我也会遇到此崩溃。
    【解决方案3】:

    如果您坚持对属性的看法,请确保它们很强大!这可能是他们为零的原因。

    【讨论】:

    • 这里也一样,切换到 strong 解决了问题。
    • @smileBot 我设置为强但返回零。我已经扩展了 UIView 和 loadNib 文件并更新了约束,但由于 self.subView 为 nil 而崩溃。 [self addConstraint:[NSLayoutConstraint constraintWithItem:self.subView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
    • @NikunjJadav 你的问题解决了吗?
    【解决方案4】:

    如果您出于某种原因关闭 Autolayout,也会发生这种情况,但您仍然通过代码向任何子视图添加约束(例如,用于处理方向更改)

    当我习惯了 iPhone 和 iPad 的不同 NIB 并在使用通用 .m 文件时关闭了仅 iPhone NIB 的自动布局时,这发生在我身上。

    解决方案是检查设备类型并在不需要的地方跳过布局添加。

    【讨论】:

    • @smileBot 我设置为强但返回零。我已经扩展了 UIView 和 loadNib 文件并更新了约束,但由于 self.subView 为 nil 而崩溃。 [self addConstraint:[NSLayoutConstraint constraintWithItem:self.subView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
    【解决方案5】:

    当我不小心删除了我的一个 UI 元素的“引用出口”时,就会发生这种情况。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      • 2017-01-19
      相关资源
      最近更新 更多