2012-12-13 15 views
7

Podklasy UICollectionViewCell i wykonuję cały układ w kodzie za pomocą automatycznego układu. To jest moja metoda startowych:Niestandardowy UICollectionViewCell Auto Layout NSInternalInconsistency Błąd wyjątku

- (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; 
} 

Kiedy wykomentuj linię translateAutoresizingMask uzyskać:

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. 

Kiedy ja nie mogę dostać ten błąd: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UICollectionView's implementation of -layoutSubviews needs to call super.' Jak mogę to pokazać tak jak tego chcę? czego mi brakuje?

+0

Czy "@" | - (20) - [tło] | "" powinno mieć literę H: z przodu, czy jest to styl składni, którego nie znam? Ponadto wygląda na to, że "pricesView" zostanie zwolniony po funkcji init ... więc masz tylko widok. Czy to spowoduje problemy? – yuf

+0

H: jest opcjonalne. Jeśli nie podano wartości H lub V, domyślnie jest to H. W tej chwili utrata kontrolera widoku jest w porządku. Próbuję tylko rozłożyć układ. – Civatrix

+0

Mam cię, nie wiedziałem o H, spoko. Czy próbowałeś naprawić błędy ograniczenia bez usuwania maski automatycznej zmiany rozmiaru? Myślę, że maska ​​jest wymagana w komórce ... Zamiast tego wykonaj backgroundView.translatesAutoresizingMaskIntoConstraints = NO; i pricesView.view.translatesAutoresizingMaskIntoConstraints = NO; – yuf

Odpowiedz

14

zamieszczaniu mój komentarz jako odpowiedź:

W moich doświadczeniach, collectionViewCells (i tableViewCells) wymagają ich autoresizingmask lub rzucają wyjątek widzisz. Ale są coraz konfliktów ograniczeń ze względu na subviews dodanych, więc po prostu zdjąć maskę z jego subviews:

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

Staram się też pamiętać, aby usunąć maskę na widoki tworzę z wykorzystaniem Alloc (czyli nie z xib), ponieważ w większości przypadków powodują konflikty.

+0

Jeszcze raz dziękuję, bardzo się uczę z nowym układem automatycznym. – Civatrix

+7

Tak naprawdę musiałem ustawić to [na 'contentView'] (https://github.com/artsy/eidolon/pull/123/files#diff-d8eaed9deb2eb4f390a6c8a637465fdbR33). –

+0

Ustawienie tego dla contentView było również konieczne dla mnie, a następnie dodawanie ograniczeń szerokości i wysokości – Philip

Powiązane problemy