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

博客作者在项目中遇到UICollectionView由于AutoLayout设置导致的UIView-Encapsulated-Layout-Width冲突问题。通过查看log和查阅Stack Overflow,了解到CollectionView会默认添加适应大小的约束,与自定义约束冲突。解决方案是调整自定义约束的优先级,使其低于默认约束,从而避免冲突。示例代码展示了如何修改约束优先级来解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原创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



原则上这种代码不会出问题的啊。于是乎我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];
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值