【问题标题】:Custom UICollectionViewCell Auto Layout NSInternalInconsistencyException error自定义 UICollectionViewCell 自动布局 NSInternalInconsistencyException 错误
【发布时间】:2012-12-01 23:26:11
【问题描述】:

我正在继承 UICollectionViewCell 并使用自动布局在代码中进行所有布局。这是我的初始化方法:

- (id)initWithFrame:(CGRect)frame{
    frame = CGRectMake(0, 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.

当我没有收到此错误时: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UICollectionView's implementation of -layoutSubviews needs to call super.' 我如何让它显示我想要的方式?我错过了什么?

【问题讨论】:

  • "@"|-(20)-[background]|"" 应该有一个 H: 在前面还是我不知道的一种语法风格?此外,看起来“pricesView”将在 init 函数之后被释放......所以你只有视图。这会导致问题吗?
  • H: 是可选的。如果既没有指定 H 也没有指定 V,则默认为 H。目前,丢失视图控制器很好。我只是想降低布局。
  • 知道了,不知道 H,很酷。您是否尝试在不删除自动调整大小掩码的情况下修复约束错误?我认为单元格中需要掩码...相反,请执行 backgroundView.translatesAutoresizingMaskIntoConstraints = NO;和 pricesView.view.translatesAutoresizingMaskIntoConstraints = NO;

标签: ios uicollectionview autolayout uicollectionviewcell


【解决方案1】:

发表我的评论作为答案:

根据我的经验,collectionViewCells(和 tableViewCells)需要它们的自动调整掩码,否则它们会抛出您看到的异常。但是由于您添加的子视图,您会遇到约束冲突,因此只需从其子视图中删除掩码:

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

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

【讨论】:

  • 我实际上必须设置这个on the contentView
  • 在 contentView 上设置这个对我来说也是必要的,然后添加宽度和高度约束
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多