Benutzerdefinierte UICollectionViewCell Auto-Layout NSInternalInconsistencyException Fehler
Ich bin Unterklassen eine UICollectionViewCell und dabei alle layout-code mit auto-layout". Das ist meine init-Methode:
- (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;
}
Wenn ich kommentiere die translateAutoresizingMask Zeile bekomme ich:
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.
Wenn ich das nicht bekomme ich diesen Fehler:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UICollectionView's implementation of -layoutSubviews needs to call super.'
Wie bekomme ich dies um zu zeigen, wie ich es will? Was bin ich?
- Ist "@"|-(20)-[hintergrund]|"" soll eine H: vor oder ist das ein Stil, der syntax, die ich nicht kenne? Auch, es sieht aus wie "pricesView" veröffentlicht werden, nachdem die init-Funktion...so haben Sie nur die Ansicht. Ist, dass Sie gehen, um Probleme zu verursachen?
- H: ist optional. Wenn weder ein H oder ein V ist angegeben, ist er standardmäßig H. Für den moment der Verlust des view-Controllers ist in Ordnung. Ich bin einfach nur versuchen, um das layout nach unten.
- Gotcha, wusste nicht, über die H, cool. Haben Sie versuchen, die Festsetzung der constraint-Fehler, ohne zu entfernen die automatische Größenänderung der Maske? Ich denke, die Maske ist erforderlich, in der Zelle...Stattdessen tun backgroundView.translatesAutoresizingMaskIntoConstraints = NEIN; und pricesView.Blick.translatesAutoresizingMaskIntoConstraints = NO;
- Das hat es gut gemacht.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Posting meinen Kommentar als Antwort:
Meinen Erfahrungen, collectionViewCells (und tableViewCells) verlangen von Ihren autoresizingmask oder Sie werfen der Ausnahme, die Sie sehen. Aber Sie werden immer constraint-Konflikte aufgrund der Untersichten, die Sie Hinzugefügt, so entfernen Sie die Maske aus seiner Untersichten:
Ich versuche auch, daran zu erinnern, um entfernen Sie die Maske auf Ansichten, die ich mit alloc (dh nicht von einem xib), da die meisten der Zeit, die Sie geben, Konflikte.
contentView
.