我有一个UICollectionViewCell,它一直给我一个错误的自动布局约束冲突。以下是它在调试器中打印出的声明它声称存在冲突的约束:
(
"<NSAutoresizingMaskLayoutConstraint:0x10e47df70 h=--& v=--& UIView:0x1094594c0.width == 414 (active)>",
"<NSAutoresizingMaskLayoutConstraint:0x10e47e110 h=--& v=--& UIView:0x1094594c0.height == 531 (active)>",
"<NSLayoutConstraint:0x10947e7e0 UIButton:0x10947e410.height == 44 (active)>",
"<NSLayoutConstraint:0x10e457fd0 UIImageView:0x10e44e960.height == 1.2*UIImageView:0x10e44e960.width (active)>",
"<NSLayoutConstraint:0x109488520 V:|-(0)-[UIImageView:0x10e44e960] (active, names: '|':UIView:0x10947bfd0 )>",
"<NSLayoutConstraint:0x109488610 H:[UIImageView:0x10e44e960]-(0)-| (active, names: '|':UIView:0x10947bfd0 )>",
"<NSLayoutConstraint:0x109488700 H:|-(0)-[UIImageView:0x10e44e960] (active, names: '|':UIView:0x10947bfd0 )>",
"<NSLayoutConstraint:0x1094890c0 V:[UIImageView:0x10e44e960]-(8)-[UITextView:0x102940a00'Hey'] (active)>",
"<NSLayoutConstraint:0x10948a520 V:[UIButton:0x10947e410]-(0)-| (active, names: '|':UIView:0x10947bfd0 )>",
"<NSLayoutConstraint:0x10948a820 V:[UITextView:0x102940a00'Hey']-(8)-[UIButton:0x10947e410] (active)>",
"<NSLayoutConstraint:0x10948ae00 V:|-(5)-[UIView:0x10947bfd0] (active, names: '|':UIView:0x109452f60 )>",
"<NSLayoutConstraint:0x10948af40 H:|-(5)-[UIView:0x10947bfd0] (active, names: '|':UIView:0x109452f60 )>",
"<NSLayoutConstraint:0x10948b030 V:[UIView:0x10947bfd0]-(5)-| (active, names: '|':UIView:0x109452f60 )>",
"<NSLayoutConstraint:0x10948b120 H:[UIView:0x10947bfd0]-(5)-| (active, names: '|':UIView:0x109452f60 )>",
"<NSLayoutConstraint:0x10948b500 H:[UIView:0x109452f60]-(0)-| (active, names: '|':UIView:0x1094594c0 )>",
"<NSLayoutConstraint:0x10948b640 H:|-(0)-[UIView:0x109452f60] (active, names: '|':UIView:0x1094594c0 )>",
"<NSLayoutConstraint:0x10948b730 V:|-(0)-[UIView:0x109452f60] (active, names: '|':UIView:0x1094594c0 )>",
"<NSLayoutConstraint:0x10948b820 V:[UIView:0x109452f60]-(0)-| (active, names: '|':UIView:0x1094594c0 )>"
)
然后通过破坏以下约束来尝试解决问题:
<NSLayoutConstraint:0x10e457fd0 UIImageView:0x10e44e960.height == 1.2*UIImageView:0x10e44e960.width (active)>
由于我的XIB没有显示任何错误,我在尝试识别确切冲突的方面遇到了麻烦。
答案 0 :(得分:1)
您有一个约束,将图像的纵横比设置为1.2,因此图像的高度必须是宽度的1.2倍。
但是您也将图像限制为与其父视图相同的大小减去某些控件的固定空间。此视图依次设置为与其父视图相同的大小减去5个单位边框,此第三个视图与最外侧视图的大小相同,其中固定大小为414 x 531。
我希望,一旦解决了这个问题,图像就不能比它的宽度高1.2倍。
我会尝试将图像的水平约束删除到其父视图边缘,并添加一个新的水平约束以使其水平居中,看看它是如何进行的。