ios 8.3 NSLayoutAttributeNotAnAttribute 崩溃

标签 ios autolayout nslayoutconstraint ios8.3

我的 NSLayoutConstraint 遇到了一个奇怪的问题。

我最近将我的 iPhone 更新到 8.3 并使用最新的 Xcode。

该应用程序在 iOS 8.3 上崩溃,断点指向我:

self.triangleTopConstraint = [NSLayoutConstraint constraintWithItem:self.triangle
                                                          attribute:NSLayoutAttributeTop
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:nil
                                                          attribute:NSLayoutAttributeTop
                                                         multiplier:1.0
                                                           constant:0.0];

[self addConstraint:self.triangleTopConstraint];

这在 iOS 8.3 之前有效。

但是,我在这里阅读了此页面:

https://developer.apple.com/library/ios/documentation/AppKit/Reference/NSLayoutConstraint_Class/#//apple_ref/c/tdef/NSLayoutAttribute

它说当关系没有第二个 View 时使用 NSLayoutAttributeNotAnAttribute。所以我把它改成这样:

self.triangleTopConstraint = [NSLayoutConstraint constraintWithItem:self.triangle
                                                          attribute:NSLayoutAttributeTop
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:nil
                                                          attribute:NSLayoutAttributeNotAnAttribute
                                                         multiplier:1.0
                                                           constant:0.0];

[self addConstraint:self.triangleTopConstraint];

但该应用程序在 iOS 8.3 上仍然崩溃。

我错过了什么吗?

最佳答案

您可以设置绝对高度或宽度值,在这种情况下,您可以使用 niltoItem 和第二个 attribute NSLayoutAttributeNotAnAttribute;但您不能设置绝对顶部。您必须设置顶部相对于其他东西——例如,相对于父 View 的顶部(它不是必须是父 View 的顶部,这是一件特别常见的事情,所以我以它为例)。

关于ios 8.3 NSLayoutAttributeNotAnAttribute 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29641579/

相关文章:

ios - 为什么CFStreamCreatePairWithSocketToHost成功后CFStreamCreatePairWithSocketToCFHost失败

ios - 当用户将焦点从文本字段更改为另一个时调用方法 (RubyMotion)

ios - 修改 UIViewController.frame 时约束发生变化

cocoa - NSView 中的自动布局 NSImageView - 缩放大小。以编程方式设置 NSLayoutConstraint

ios - 以编程方式在 UINavigationBar 中添加布局约束

ios - iPad 上的空应用程序图标仅适用于 iPhone 应用程序

iphone - View 似乎会自行重新加载

uitableview - UIStackview 在 UITableviewCell 中按比例填充采用不正确的框架

ios - 如何使用自动布局相对于 UIView 高度移动 UILabel?

ios - 如何添加约束以右对齐 UITableViewCell 内的图像