HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 自定义UICollectionViewCell自动布局NSInternalInconsistencyException错误大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在继承UICollectionViewCell并使用自动布局在代码中进行所有布局.这是我的init方法
- (id)initWithFrame:(CGRect)frame{
    frame = CGRectMake(0,403,533);
    if (self = [super initWithFrame:frame]) {
        self.translatesAutoresizingMaskIntoConsTraints = NO;

        PBCardPricesViewController *pricesView = [[PBCardPricesViewController alloc] init];
        [self addSubview:pricesView.view];

        UIImageView *BACkground = [[UIImageView alloc] initWithImage:[UIImage imagenamed:@"CardBACkground"]];
        [self addSubview:BACkground];

        [self addConsTraints:[NSLayoutConsTraint consTraintsWithVisualFormat:@"|-(20)-[BACkground]|" options:0 metrics:nil views:@{@"BACkground":BACkgrounD}]];
        [self addConsTraint:[NSLayoutConsTraint consTraintWithItem:pricesView.view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:BACkground attribute:NSLayoutAttributeLeft multiplier:1 constant:0]];

        [self addConsTraints:[NSLayoutConsTraint consTraintsWithVisualFormat:@"V:|[BACkground]|" options:0 metrics:nil views:@{@"BACkground":BACkgrounD}]];
        [self addConsTraint:[NSLayoutConsTraint consTraintWithItem:pricesView.view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:BACkground attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
    }

    return self;
}

当我注释掉translateAutoresizingMask行时,我得到:

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. (Note: If you're seeing NSAutoresizingMaskLayoutConsTraints that you don't understand,refer to the documentation for the UIView property translatesAutoresizingMaskIntoConsTraints) 

(

    "<NSLayoutConsTraint:0x1d83f540 H:[UIImageView:0x1d83f950]-(0)-|   (Names: '|':PBCardViewCollectionCell:0x1d83b970 )>","<NSAutoresizingMaskLayoutConsTraint:0x1c55ad20 h=--& v=--& H:[PBCardViewCollectionCell:0x1d83b970(393)]>","<NSAutoresizingMaskLayoutConsTraint:0x1c559410 h=--& v=--& UIImageView:0x1d83f950.midX == + 191.5>","<NSAutoresizingMaskLayoutConsTraint:0x1c559450 h=--& v=--& H:[UIImageView:0x1d83f950(383)]>"

)



Will attempt to recover by breaking consTraint 

<NSLayoutConsTraint:0x1d83f540 H:[UIImageView:0x1d83f950]-(0)-|   (Names: '|':PBCardViewCollectionCell:0x1d83b970 )>



Break on objc_exception_throw to catch this in the debugger.

The methods in the UIConsTraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be Helpful.

当我不这样做时,我收到此错误
由于未捕获的异常’NSInternalInconsistencyException’而终止应用程序,原因:’执行-layoutSubviews后仍需要自动布局. UICollectionView的-layoutSubviews实现需要调用super.
如何让它显示我想要的方式?我错过了什么?

解决方法@H_301_14@
发表我的评论作为答案:

根据我的经验,collectionViewCells(和tableViewCells)需要他们的autoresizingmask或他们抛出你看到的异常.但是由于您添加的子视图,您正在遇到约束冲突,因此只需从其子视图中删除掩码:

BACkgroundView.translatesAutoresizingMaskIntoConsTraints = NO;  
pricesView.view.translatesAutoresizingMaskIntoConsTraints = NO; // you might get it to work without doing this line

我还尝试记住删除我使用alloc创建的视图上的掩码(即不是来自xib),因为大多数时候它们都会产生冲突.

大佬总结

以上是大佬教程为你收集整理的ios – 自定义UICollectionViewCell自动布局NSInternalInconsistencyException错误全部内容,希望文章能够帮你解决ios – 自定义UICollectionViewCell自动布局NSInternalInconsistencyException错误所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。