约束错误<will attempt =“”to =“”recover =“”by =“”breaking =“”constraint =“”>

时间:2015-06-10 06:44:23

标签: ios autolayout

这是我的UICollectionViewCell图片...

enter image description here

我将自动布局约束添加到此单元格...但它给了我很多破坏约束错误....然后我决定一步一步解决制动约束错误。所以我只是给出了约束条件 UIImageView和标题标签,控制台日志为....

 (
"<NSLayoutConstraint:0x158cec50 H:[UIImageView:0x158de490(100)]>",
"<NSLayoutConstraint:0x1582bd30 H:|-(8)-[UIImageView:0x158de490]   (Names: '|':UIView:0x158db340 )>",
"<NSLayoutConstraint:0x1582bd90 H:[UILabel:0x158de540'Rhythm | Responsive Multi...']-(8)-|   (Names: '|':UIView:0x158db340 )>",
"<NSLayoutConstraint:0x1582b200 H:[UIImageView:0x158de490]-(7)-[UILabel:0x158de540'Rhythm | Responsive Multi...']>",
"<NSLayoutConstraint:0x15800a00 H:[UIView:0x158db340]-(0)-|   (Names: '|':UIView:0x158cea10 )>",
"<NSLayoutConstraint:0x15800a60 H:|-(0)-[UIView:0x158db340]   (Names: '|':UIView:0x158cea10 )>",
"<NSAutoresizingMaskLayoutConstraint:0x147cfe50 h=--& v=--& H:[UIView:0x158cea10(50)]>"
 )

 Will attempt to recover by breaking constraint 
 <NSLayoutConstraint:0x158cec50 H:[UIImageView:0x158de490(100)]>   

这是我的 UIImageView约束和我的标题标签约束   enter image description here enter image description here

我希望我的UIImageview宽度为100 ..所以当我刚刚为图像添加上述约束时...它很好但是只要我添加了标签的前导约束...它会导致问题......我想要标签宽度动态

那么,我该如何解决这个错误...

修改
我的视图层次结构

-Cell
   - View 
       - Image  
       - Title  

解决
   这2行解决了这个问题......

 cell.contentView.frame = cell.bounds
 cell.contentView.autoresizingMask = UIViewAutoresizing.FlexibleRightMargin | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleTopMargin  

关注
但是我想知道为什么会这样....因为在一些具有相同布局和相同约束的项目中,它的工作正常,而在某些项目中却没有...... 为什么???

1 个答案:

答案 0 :(得分:0)

将约束放在屏幕截图下面

enter image description here

enter image description here

enter image description here