[英]Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter
[英]Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: Unknown layout attribute'
我試圖以編程方式將約束添加到UIView
內的按鈕:
let constraint = NSLayoutConstraint(item: arrowButton, attribute: NSLayoutAttribute.LeadingMargin, relatedBy: NSLayoutRelation.Equal,
toItem: self, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 10)
arrowButton.addConstraint(constraint)
我希望按鈕的前邊距距離視圖的左邊距為10。 此代碼在視圖內部運行,因此self
是視圖。 上面的代碼給出了錯誤:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: Unknown layout attribute'
我究竟做錯了什么?
NSLayoutAttributeNotAnAttribute:
占位符值,用於指示約束的第二個項目和第二個屬性未在任何計算中使用。 在創建為屬性指定常量的約束時使用此值。 例如,item1.height> = 40. 如果約束只有一個項目,則將第二個項目設置為nil,並將第二個屬性設置為NSLayoutAttributeNotAnAttribute。
你有兩個項目: arrowButton
和self
,但你指定NSLayoutAttributeNotAnAttribute
說我只需要一個項目。 這就是它失敗的原因。
我建議您先使用storybard或xib來練習自動布局,然后再感覺它是如何工作的,然后開始編碼。 你明顯誤解了自動布局。 如果要定義兩個視圖的布局關系,則NSLayoutAttributeNotAnAttribute
將永遠無效。
NSLayoutAttribute.NotAnAttribute
,
我想這就是問題所在。 您傳入NotAnAttribute
此外,它應該是self.addConstraint(constraint)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.