2012-12-13 21 views
7

私はUICollectionViewCellをサブクラス化し、自動レイアウトでコード内のすべてのレイアウトを実行しています。これは私のinitメソッドである:カスタムUICollectionViewCell自動レイアウトNSInternalInconsistencyExceptionエラー

私は私が手translateAutoresizingMaskラインコメントアウト
- (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; 
} 

:私はショーにこれを取得するにはどうすればよい Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UICollectionView's implementation of -layoutSubviews needs to call super.' :私は、私はこのエラーを取得しない場合は

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. 

を私はそれをしたい方法ですか?私は何が欠けていますか?

+0

"@" | - (20) - [背景] | ""先頭にH:を付けるか、これはわからない構文のスタイルですか?また、 "pricesView"がinit関数の後にリリースされるように見えるので、あなたはビューしか持っていません。それは問題を引き起こすだろうか? – yuf

+0

H:はオプションです。 HまたはVのどちらも指定されていない場合は、デフォルトでHになります。瞬間的にView Controllerの損失は問題ありません。私はレイアウトを下ろそうとしているだけです。 – Civatrix

+0

Gotcha、Hについて知りませんでした。自動サイズ変更マスクを削除せずに制約エラーを修正しようとしましたか?私はマスクがセルで必要と思う...その代わりに、backgroundView.translatesAutoresizingMaskIntoConstraints = NO;およびpricesView.view.translatesAutoresizingMaskIntoConstraints = NO; – yuf

答えて

14

答えとして私のコメント投稿:私の経験では

を、collectionViewCells(およびtableViewCellsは)自分のautoresizingmaskを必要とするか、または、彼らはあなたが見例外をスローします。しかし、あなたはこれだけのサブビューからマスクを削除、追加したサブビューに起因する制約競合を取得している:

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

私も、私はアロケーションを使用して作成したビューの上にマスクを削除することを忘れないようにしようと(すなわちないXIBから)、ほとんどの時間彼らは紛争を与えるので。

+0

もう一度新しい自動レイアウトで学ぶことに感謝します。 – Civatrix

+7

私は実際にこれを[contentView'](https://github.com/artsy/eidolon/pull/123/files#diff-d8eaed9deb2eb4f390a6c8a637465fdbR33)に設定しなければなりませんでした。 –

+0

これをcontentViewに設定することも必要でしたが、幅と高さの制約を追加しました – Philip

関連する問題