1. 程式人生 > >【已解決】xcode autoLayout約束佈局問題解決

【已解決】xcode autoLayout約束佈局問題解決

連結:https://www.jianshu.com/p/737bf71c4dd9

在專案中使用了autoLayout來對自定義的cell進行約束。cell可以正常顯示, 看是控制檯列印瞭如下報錯資訊:

Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this:
(1) look at each constraint and try to figure out which you don't expect; 
(2) find the code that added the unwanted constraint or constraints and fix it.

( "<NSLayoutConstraint:0x7fbb85999770 V:[UIView:0x7fbb85e8f850(1)]>", 
"<NSLayoutConstraint:0x7fbb85e24e80 V:[UIView:0x7fbb85e8f570(1)]>", 
"<NSLayoutConstraint:0x7fbb85e6f500 V:[UIImageView:0x7fbb85e79b80(200)]>",
"<NSLayoutConstraint:0x7fbb83758b80 V:[UIView:0x7fbb85da7610(44)]>", 
"<NSLayoutConstraint:0x7fbb8375a240 V:|-(0)-[UIView:0x7fbb85e8f850] (Names: '|':UIView:0x7fbb85e78d40 )>",
"<NSLayoutConstraint:0x7fbb8375a330 V:[UIView:0x7fbb85e8f850]-(20)-[UILabel:0x7fbb85e7e8a0'\U5c0f\U5446\U74dc\U6eda\U5440\U6eda']>", 
"<NSLayoutConstraint:0x7fbb8375a470 V:[UILabel:0x7fbb85e7e8a0'\U5c0f\U5446\U74dc\U6eda\U5440\U6eda']-(5)-[UILabel:0x7fbb8599abf0'\U6765\U81ea \U5c0f\U7ec4[\U60a6\U53cb\U4f1a]']>", 
"<NSLayoutConstraint:0x7fbb8375a510 V:[UILabel:0x7fbb8599abf0'\U6765\U81ea \U5c0f\U7ec4[\U60a6\U53cb\U4f1a]']-(12)-[UILabel:0x7fbb85e807d0'\U7f8e\U56fd\Uff0c\U6cd5\U56fd\U548c\U4e2d\U56fd\U5973\U4eba\U8981\U51cf\U80a5\U7684\U65f6\U5019']>", 
"<NSLayoutConstraint:0x7fbb8375a650 V:[UILabel:0x7fbb85e807d0'\U7f8e\U56fd\Uff0c\U6cd5\U56fd\U548c\U4e2d\U56fd\U5973\U4eba\U8981\U51cf\U80a5\U7684\U65f6\U5019']-(12)-[UIView:0x7fbb85e8f570]>", 
"<NSLayoutConstraint:0x7fbb8375a740 V:[UIView:0x7fbb85e8f570]-(12)-[UILabel:0x7fbb85e7fba0'\U7f8e\U56fd']>", 
"<NSLayoutConstraint:0x7fbb8375a790 V:[UILabel:0x7fbb85e7fba0'\U7f8e\U56fd']-(12)-[UIImageView:0x7fbb85e79b80]>", 
"<NSLayoutConstraint:0x7fbb8375a880 V:[UIImageView:0x7fbb85e79b80]-(3)-[UILabel:0x7fbb85e90010]>", 
"<NSLayoutConstraint:0x7fbb8375a8d0 V:[UILabel:0x7fbb85e90010]-(0)-[UIView:0x7fbb85da7610]>", 
"<NSLayoutConstraint:0x7fbb8375a920 V:[UIView:0x7fbb85da7610]-(0)-| (Names: '|':UIView:0x7fbb85e78d40 )>", 
"<NSLayoutConstraint:0x7fbb8375b0d0 V:|-(0)-[UIView:0x7fbb85e78d40] (Names: '|':UITableViewCellContentView:0x7fbb859a4e90 )>", 
"<NSLayoutConstraint:0x7fbb8375b170 UITableViewCellContentView:0x7fbb859a4e90.bottomMargin == UIView:0x7fbb85e78d40.bottom - 8>", 
"<NSLayoutConstraint:0x7fbb85a75650 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fbb859a4e90(297.5)]>"`` )

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fbb85e6f500 V:[UIImageView:0x7fbb85e79b80(200)]>



看到開頭的報錯資訊, 是說不能同時滿足我的約束條件,讓我從下面列出的的約束條件中找出我不想要的,然後修復它。 於是我就挨個檢查了每個約束條件, 去掉了不必要的約束條件, 但仍是一直報錯。 最後發現了這句話:
Will attempt to recover by breaking constraint這句話是說嘗試打破下面的約束來進行修復, 這才找到問題所在。 對於這個這個UIImageView 我在cell中約束的值的高度是100,但是在實際的使用中,它的實際值卻是200 ,所以會有約束衝突報錯。
解決的辦法就是降低這個約束的優先順序(Priority),當系統實際計算 的高度與我設定的約束值不一樣時, 取系統的值, 就不會再報錯了。
修改方式如下:
選中要修改的約束,將其值調低(999)即可。