2015-04-15 153 views
3

我得到了一個奇怪的問題,我的NSLayoutConstraint。ios 8.3 NSLayoutAttributeNotAnAttribute崩潰

我最近更新了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上崩潰。

我錯過了什麼嗎?

+0

它「正在工作」如何?你永遠不可能將事物的_top_設置爲絕對值。我不知道你的代碼在幹什麼,但總是錯的。 – matt

+0

我有一個叫做三角形的子視圖,它是自定義視圖的子視圖。我正在使用此動畫約束來更改三角形子視圖的Y座標。我將如何使用正確的方式去實現我想要的東西,請賜教matt – Zhang

+0

您設置'self.triangle'的頂部__與其他東西相關,比如它的頂層視圖(它不會被_have_成爲superview的頂部,這只是一個特別常見的事情,所以我用它作爲例子)。 – matt

回答

4

您可以設置絕對高度或寬度值,在這種情況下,您將使用toItemnil和第二個attributeNSLayoutAttributeNotAnAttribute;但你不能設置絕對頂部。你必須設置頂部相對於其他的東西 - 例如,關於超級視圖的頂部(它不是是超級視圖的頂部,這只是一個特別常見的事情,所以我用它作爲例子)。

+0

只有寬度或高度可以是恆定的,並且不涉及任何項目。 –

+0

順便說一下,除了firstItem是寬度或高度之外,不允許將倍數設置爲零。否則它會引發一個例外,就像@張說的那樣。 –