2013-03-07 4 views
5

मैं आईबी में autolayout की कमी उपयोग करने से पहले की कोशिश की है करने के लिए NSLayoutConstraints को लागू करने और मैं समझता हूँ कि वे कैसे काम करते हैं और है कि वे आदि स्पष्ट करने की आवश्यकता है ...एक UIView उपवर्ग

लेकिन आप विचारों के एक जोड़े की तुलना में अधिक है जब यह थोड़ा जटिल हो जाता है और टूटने लगता है।

तो, मैंने एक समस्या के बारे में एक ब्लॉग पढ़ा है और इसके बजाय बाधाओं को बनाने के लिए ASCII कोड का उपयोग कर रहा है।

मेरे पास एक कस्टम UITableViewCell के साथ एक UITableView है जिसमें सामग्री की मात्रा के आधार पर गतिशील आकार है। ऑटो लेआउट के लिए एक आदर्श उम्मीदवार।

तो, मैंने इसे करने के कई तरीकों की कोशिश की है और अब मैंने सामग्री को काट दिया है ताकि सेल में केवल एक ही लेबल हो।

मैं क्या चाहूंगा कि लेबल के चारों ओर एक सीमा के साथ सेल भरने के लिए।

यानी सेल के प्रत्येक किनारे से मानक आकार बिंदु।

मैं इसे इस तरह किया है ...

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 10)]; 
     _label.textColor = [UIColor colorWithWhite:0.53 alpha:1.0]; 
     _label.backgroundColor = [UIColor yellowColor]; 
     [self addSubview:_label]; 

     NSDictionary *views = NSDictionaryOfVariableBindings(_label); 

     [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[_label]-|" 
                    options:0 
                    metrics:nil 
                     views:views]]; 

     [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[_label]-|" 
                    options:0 
                    metrics:nil 
                     views:views]]; 

     self.backgroundView = [[UIView alloc] initWithFrame:CGRectZero]; 
     self.backgroundColor = [UIColor clearColor]; 
    } 
    return self; 
} 

लेकिन जब सेल प्रदर्शित किया जाता है लेबल ऊपरी बाएं कोने बिल्कुल CGRect जैसे मैं इसे init करने के लिए इस्तेमाल में है और मैं इस भार प्राप्त बाधाओं के बारे में त्रुटियों की ...

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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x1f859e40 h=--& v=--& UILabel:0x1f864a00.midX == + 5>", 
    "<NSLayoutConstraint:0x1f86a4f0 H:|-(NSSpace(20))-[UILabel:0x1f864a00] (Names: '|':MyCell:0x1f857740)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1f86a4f0 H:|-(NSSpace(20))-[UILabel:0x1f864a00] (Names: '|':MyCell:0x1f857740)> 

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. 
2013-03-07 11:56:14.841 unasys[13082:907] 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:0x1f85f7b0 V:|-(NSSpace(20))-[UILabel:0x1f864a00] (Names: '|':MyCell:0x1f857740)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1f859ec0 h=--& v=--& UILabel:0x1f864a00.midY == + 5>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1f85f7b0 V:|-(NSSpace(20))-[UILabel:0x1f864a00] (Names: '|':MyCell:0x1f857740)> 

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. 
2013-03-07 11:56:14.854 unasys[13082:907] 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:0x1f86a540 H:[UILabel:0x1f864a00]-(NSSpace(20))-| (Names: '|':MyCell:0x1f857740)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1f859e40 h=--& v=--& UILabel:0x1f864a00.midX == + 5>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1f859e80 h=--& v=--& H:[UILabel:0x1f864a00(10)]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1ed8e150 h=--& v=--& H:[MyCell:0x1f857740(320)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1f86a540 H:[UILabel:0x1f864a00]-(NSSpace(20))-| (Names: '|':MyCell:0x1f857740)> 

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. 
2013-03-07 11:56:14.858 unasys[13082:907] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x1eda7260 h=--& v=--& V:[MyCell:0x1f857740(143)]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1f859f00 h=--& v=--& V:[UILabel:0x1f864a00(10)]>", 
    "<NSLayoutConstraint:0x1f85f870 V:[UILabel:0x1f864a00]-(NSSpace(20))-| (Names: '|':MyCell:0x1f857740)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1f859ec0 h=--& v=--& UILabel:0x1f864a00.midY == + 5>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1f85f870 V:[UILabel:0x1f864a00]-(NSSpace(20))-| (Names: '|':MyCell:0x1f857740)> 

और मुझे नहीं पता कि यह डिबगिंग के साथ कहां से शुरू करना है। बाधाएं बिल्कुल कम हैं लेकिन फिर भी यह पूरी तरह से विफल हो जाती है।

क्या कोई मुझे इस दिशा में काम करने के लिए सही दिशा में इंगित कर सकता है?

उत्तर

13

लाइन के बाद

_label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 10)]; 

जोड़ने

[_label setTranslatesAutoresizingMaskIntoConstraints:NO]; 

डिफ़ॉल्ट रूप से autoresizingmask भी बाधाओं में बदल गया है, जो की कमी आप द्वारा तय किये का विरोध करता है, जिससे यह ambigious

दस्तावेज़ यहां हैं: setTranslatesAutoresizingMaskIntoConstraints

+0

बहुत बढ़िया! बहुत बहुत धन्यवाद! – Fogmeister

+0

काम करता है! मुझे self.content में सामान जोड़ने के लिए भी बदलना पड़ा क्योंकि स्वयं के बजाय एक UITableViewCell है और फिर यह सब काम किया: डी धन्यवाद! – Fogmeister

+1

मूल रूप से यदि आप एक ऑटोलायआउट त्रुटि में 'NSAutoresizingMaskLayoutConstraint' देखते हैं और आपको नहीं पता कि यह क्यों है, तो आप कहीं भी – iain