2012-09-17 27 views
14

我有我的LayoutConstraints工作正常,然後突然間,我開始得到這個時,添加約束到我的看法。約束必須包含第一個佈局項NSLayoutConstraint

*** 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 

還有互聯網的蘋果文檔上或不存在直接的文檔說明錯誤消息意味着什麼。

想知道是否有人遇到這個錯誤,並知道我能做些什麼來解決它?

回答

21

啊,發現問題了。 對於任何有此問題的人來說,這是因爲該視圖尚未在nib/UIView中創建,所以不能應用任何約束。

我將約束代碼從initWithNib方法移至viewDidLoad,錯誤自然停止發生。

+0

即使約束在viewDidLoad中創建/添加,我也會收到崩潰。 –

+0

向我們展示您的代碼,這將是簡單的 –

33

此錯誤/崩潰的另一個原因可能是,傳遞給[NSLayoutContraint constraintWithItem ...]方法的視圖爲零。

+0

@smileBot我設置爲強壯但返回零。我已經擴展了UIView和loadNib文件並更新了約束,但由於self.subView而導致崩潰。 [self addConstraint:[NSLayoutConstraint constraintWithItem:self.subView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]]; –

0

如果您關閉自動佈局由於某種原因,這也有可能發生,但你仍然可以通過代碼添加約束任何子視圖(如處理方向變化)

這發生在我身上時,我用不同的NIB的對於iPhone和iPad,並在使用常見的.m文件時僅關閉iPhone NIB的Autolay。

解決方案是檢查設備類型並跳過不需要的佈局添加。

+0

@smileBot我設置爲強,但返回零。我已經擴展了UIView和loadNib文件並更新了約束,但由於self.subView而導致崩潰。 [self addConstraint:[NSLayoutConstraint constraintWithItem:self.subView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]]; –

7

如果你對屬性的看法一直堅持,確保它們很強大!這可以解釋爲零。

+0

切換到強固定我的問題。謝謝! –

+0

這裏一樣,切換到強固定的問題。 – blackmoon

+0

@smileBot我設置爲強壯,但返回零。我已經擴展了UIView和loadNib文件並更新了約束,但由於self.subView而導致崩潰。 [self addConstraint:[NSLayoutConstraint constraintWithItem:self.subView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]]; –

相關問題