ios – 自定义UICollectionViewCell自动布局NSInternalInconsistencyException错误

前端之家收集整理的这篇文章主要介绍了ios – 自定义UICollectionViewCell自动布局NSInternalInconsistencyException错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在继承UICollectionViewCell并使用自动布局在代码中进行所有布局.这是我的init方法
  1. - (id)initWithFrame:(CGRect)frame{
  2. frame = CGRectMake(0,403,533);
  3. if (self = [super initWithFrame:frame]) {
  4. self.translatesAutoresizingMaskIntoConstraints = NO;
  5.  
  6. PBCardPricesViewController *pricesView = [[PBCardPricesViewController alloc] init];
  7. [self addSubview:pricesView.view];
  8.  
  9. UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CardBackground"]];
  10. [self addSubview:background];
  11.  
  12. [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-(20)-[background]|" options:0 metrics:nil views:@{@"background":background}]];
  13. [self addConstraint:[NSLayoutConstraint constraintWithItem:pricesView.view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:background attribute:NSLayoutAttributeLeft multiplier:1 constant:0]];
  14.  
  15. [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[background]|" options:0 metrics:nil views:@{@"background":background}]];
  16. [self addConstraint:[NSLayoutConstraint constraintWithItem:pricesView.view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:background attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
  17. }
  18.  
  19. return self;
  20. }

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

  1. Unable to simultaneously satisfy constraints.
  2.  
  3. 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)
  4.  
  5. (
  6.  
  7. "<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)]>"
  8.  
  9. )
  10.  
  11.  
  12.  
  13. Will attempt to recover by breaking constraint
  14.  
  15. <NSLayoutConstraint:0x1d83f540 H:[UIImageView:0x1d83f950]-(0)-| (Names: '|':PBCardViewCollectionCell:0x1d83b970 )>
  16.  
  17.  
  18.  
  19. Break on objc_exception_throw to catch this in the debugger.
  20.  
  21. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

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

解决方法

发表我的评论作为答案:

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

  1. backgroundView.translatesAutoresizingMaskIntoConstraints = NO;
  2. pricesView.view.translatesAutoresizingMaskIntoConstraints = NO; // you might get it to work without doing this line

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

猜你在找的iOS相关文章