iOS AutoLayout报错包含UIView-Encapsulated-Layout-Width

原创Blog,转载请注明出处
blog.csdn.net/hello_hwc

这是今天做项目的时候遇到的一个问题,这里写下来,希望以后有人遇到了相似问题也能够快速解决。

先看看我的log

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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7fe1ac9600a0 H:|-(103)-[UICollectionView:0x7fe1ab938c00] (Names: '|':HorizontalScrollTableviewHeader:0x7fe1aa68f120 )>",
    "<NSLayoutConstraint:0x7fe1ac95fe70 H:[UICollectionView:0x7fe1ab938c00]-(0)-| (Names: '|':HorizontalScrollTableviewHeader:0x7fe1aa68f120 )>",
    "<NSLayoutConstraint:0x7fe1ac966a00 H:|-(0)-[HorizontalScrollTableviewHeader:0x7fe1aa68f120] (Names: '|':TodayNewStampApplyHeader:0x7fe1ac87aab0 )>",
    "<NSLayoutConstraint:0x7fe1ac966a50 H:[HorizontalScrollTableviewHeader:0x7fe1aa68f120]-(0)-| (Names: '|':TodayNewStampApplyHeader:0x7fe1ac87aab0 )>",
    "<NSLayoutConstraint:0x7fe1aa691d00 'UIView-Encapsulated-Layout-Width' H:[TodayNewStampApplyHeader:0x7fe1ac87aab0(0)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fe1ac95fe70 H:[UICollectionView:0x7fe1ab938c00]-(0)-|   (Names: '|':HorizontalScrollTableviewHeader:0x7fe1aa68f120 )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

定位到相关代码

  NSArray * collectionConstraints_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_collectionview]-0-|" options:0 metrics:nil views:viewsDic];
    [self addConstraints:collectionConstraints_V];
    NSArray * collectionConstraints_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-103-[_collectionview]-0-|"  options:0 metrics:nil views:viewsDic];
    [self addConstraints:collectionConstraints_H];

这里的`_collectionview`是一个UICollectionView

iOS AutoLayout报错包含UIView-Encapsulated-Layout-Width_第1张图片
原则上这种代码不会出问题的啊。于是乎我stackoverflow一下。发现这个答案和我的很相似。原理是这样的

  • CollectionView会默认添加两个约束就是UIView-Encapsulated-Layout-WidthUIView-Encapsulated-Layout-Hight保证大小适中。例如,我在我的约束里面添加了左右都对齐到Superview,可能这样做后宽度是210.1,而默认添加的约束会根据Itemsize取整为200。这样两个约束就发生冲突了。

解决方案

修改优先级,让自己的创建的冲突约束优先级低一些

例如我的代码改成这样子就没问题了

  NSArray * collectionConstraints_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_collectionview]-0-|" options:0 metrics:nil views:viewsDic];
    [self addConstraints:collectionConstraints_V];
    NSArray * collectionConstraints_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-103-[_collectionview]" options:0 metrics:nil views:viewsDic];
    [self addConstraints:collectionConstraints_H];
    NSLayoutConstraint * constraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[_collectionview]-0-|" options:0 metrics:nil views:viewsDic].firstObject;
    constraint.priority = UILayoutPriorityDefaultLow;
    [self addConstraint:constraint];

你可能感兴趣的:(iOS AutoLayout报错包含UIView-Encapsulated-Layout-Width)