iOS,用编程约束替换出口约束:IBNSLayoutConstraint 问题

iOS, Substituting outlet constraint for programmatic one: IBNSLayoutConstraint issue

我正在阅读 Big Nerd Ranch,第 20 章的最后,iOS 教科书,他们试图用 widthheight 的两个约束替换imageView 调用 thumbnailView 用于一个编程约束和 height 约束:

NSLayoutConstraint *constraint =
    [NSLayoutConstraint constraintWithItem:self.thumbnailView
                                 attribute:NSLayoutAttributeHeight
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:self.thumbnailView
                                 attribute:NSLayoutAttributeWidth
                                multiplier:1
                                  constant:0];
[self.thumbnailView addConstraint:constraint];

因此,他们删除了一个约束 属性:@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewWidthConstraint(在 class 中,而不是在 .xib) 中,留下 @property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewHeightConstraint。在各自的.xib 他们检查 Remove at build time 是否有 thumbnailView 约束的 width 并且程序开始崩溃(它在使用两个约束的出口之前工作得很好)并出现以下错误(在 Xcode7):

Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'Could not instantiate class named IBNSLayoutConstraint'

如果我取消选中 remove at build time 它会崩溃并出现以下错误:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<BNRItemCell 0x7ba87800> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key imageViewWidthConstraint.'

widthheight 约束出口用于 class 以适应用户的字体大小变化。

如果有人能解释为什么程序开始崩溃以及如何修复它,那就太好了。

您从代码中删除了插座,但没有在故事板(或 xib)中断开它。

按住 Control 键并单击文档大纲中的视图控制器以查看失效连接:

那个小小的黄色小三角形告诉你 Xcode 足够聪明,知道没有名为 imageViewWidthConstraint 的出口,但太愚蠢了,无法在情节提要场景中发出警告,甚至编译期间的警告。

点击连接上的✖︎断开连接。

顺便说一句,您现在可以直接在情节提要或 xib 中创建宽高比约束,但我猜您在写那本书时无法回头。