2012-12-13 14 views
7

मैं एक UICollectionViewCell subclassing और ऑटो लेआउट के साथ कोड में सभी लेआउट कर रहा हूँ। यह मेरा init विधि है:कस्टम UICollectionViewCell ऑटो लेआउट NSInternalInconsistencyException त्रुटि

- (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.' मैं इस दिखाने के लिए मिलता है जिस तरह से मैं इसे चाहता हूँ? मैं क्या खो रहा हूँ?

+0

"@" | - (20) - [पृष्ठभूमि] | "" एच होना चाहिए: सामने या यह वाक्यविन्यास की एक शैली है, मुझे नहीं पता? साथ ही, ऐसा लगता है कि "कीमत दृश्य" init फ़ंक्शन के बाद रिलीज़ हो जाएगा ... इसलिए आपके पास केवल दृश्य है। क्या यह समस्याएं पैदा करेगा? – yuf

+0

एच: वैकल्पिक है। यदि न तो कोई एच या वी निर्दिष्ट है तो यह एच के लिए डिफ़ॉल्ट है। इस पल के लिए दृश्य नियंत्रक का नुकसान ठीक है। मैं सिर्फ लेआउट नीचे लेने की कोशिश कर रहा हूँ। – Civatrix

+0

गोचा, एच, कूल के बारे में नहीं पता था। क्या आपने ऑटो आकार बदलने वाले मुखौटा को हटाए बिना बाधा त्रुटियों को ठीक करने का प्रयास किया था? मुझे लगता है कि सेल में मास्क की आवश्यकता है ... इसके बजाय, पृष्ठभूमि देखें .translatesAutoresizingMaskIntoConstraints = नहीं; और कीमतें देखें .view.translatesAutoresizingMaskIntoConstraints = नहीं; – yuf

उत्तर

14

पोस्ट करना जवाब के रूप में मेरी टिप्पणी:

अपने अनुभवों में, collectionViewCells (और tableViewCells) उनके autoresizingmask आवश्यकता होती है या वे अपवाद जैसा कि आप देख फेंक देते हैं। एक xib से नहीं यानी

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

मैं भी विचारों मैं alloc उपयोग करके बनाया है पर नकाब हटाने के लिए याद करने की कोशिश (: लेकिन आप subviews आपके द्वारा जोड़ी की वजह से बाधा संघर्ष हो रहे हैं, तो बस अपने subviews से नकाब हटाने), क्योंकि ज्यादातर समय वे संघर्ष देते हैं।

+0

फिर से धन्यवाद, नए ऑटो लेआउट के साथ सीखने के लिए बहुत कुछ। – Civatrix

+7

मुझे वास्तव में इसे [ContentView'] पर सेट करना पड़ा था (https://github.com/artsy/eidolon/pull/123/files#diff-d8eaed9deb2eb4f390a6c8a637465fdbR33)। –

+0

सामग्री पर इसे सेट करना मेरे लिए भी आवश्यक था, फिर चौड़ाई और ऊंचाई बाधाओं को जोड़ना – Philip

संबंधित मुद्दे