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

它说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 上崩溃。

我错过了什么吗?

4

1 回答 1

4

您可以设置绝对高度或宽度值,在这种情况下,您将使用 a toItemofnil和第二个attributeof NSLayoutAttributeNotAnAttribute; 但您不能设置绝对最高值。您必须设置与其他东西相关的顶部-例如,相对于超级视图的顶部(它不会超级视图的顶部,这只是一个特别常见的事情,所以我正在使用它举个例子)。

于 2015-04-15T04:50:10.833 回答