2013-06-21 9 views
9

मैं आईओएस में ऑटो लेआउट के लिए नया हूँ। मुझे वास्तव में अवधारणा में अवधारणा पसंद है, लेकिन यह मुझे सबसे आसान काम करने की कोशिश कर रहे पागल हो रहा है। मुझे संदेह है कि मैं अभी भी कुछ सरल अंतर्निहित सिद्धांत खो रहा हूं। मैं वास्तव में एक ऐप में इसके साथ काम करने से पहले मूल बातें करने और सीखने की कोशिश कर रहा हूं, इसलिए मैं बहुत ही सरल परीक्षण परियोजनाएं बना रहा हूं। यहां एक सरल है जैसा कि यह मिलता है जो अपेक्षा के अनुसार काम नहीं करता है। पहला हिस्सा जो काम करता है। आईबी में, मैं एक दृश्य को जोड़ने के पूरे ViewController को भरने के लिए, और XCode स्वचालित रूप से/शीर्ष/ऊपर से नीचे की कमी सेट अग्रणी/ट्रेलिंग और 0 पर अंतरिक्ष जब आईबी के साथ किया, यह उम्मीद के रूप में काम करता है:आईबी में उन्हें सेट करने से प्रोग्रामेटिक रूप से अलग-अलग बाधाओं को सेट करना?

enter image description here

enter image description here

ग्रेट करने के लिए घूमता है!

अब मैं कोड में एक ही बात करने के लिए कोशिश कर रहा हूँ:

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
UIView *redView = [[UIView alloc] initWithFrame:self.view.bounds]; 
redView.backgroundColor = [UIColor redColor]; 
[self.view addSubview:redView]; 

redView.translatesAutoresizingMaskIntoConstraints = NO; 

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f]]; 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f]]; 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f]]; 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f]]; 
} 

सभी कोड है कि वहाँ एक एकल दृश्य ऐप्स के लिए डिफ़ॉल्ट कोड के अलावा अन्य है।

जब मैं ऊपर कोड चलाने के लिए, एक ही बात को प्रतिबिंबित करने के रूप में मैं आईबी के साथ प्रोग्राम के रूप में मिलता है की कोशिश कर रहा है, मैं रोटेशन के बाद इस मिल:

enter image description here

कैसे आ है कि एक ही कमी भिन्न परिणाम मिल? शायद यह वास्तव में कुछ सरल और शर्मनाक बेवकूफ है कि मैं याद कर रहा हूं। मदद!!!

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[redView]|" 
                    options:0 
                    metrics:nil 
                    views:NSDictionaryOfVariableBindings(redView)]]; 
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[redView]|" 
                    options:0 
                    metrics:nil 
                    views:NSDictionaryOfVariableBindings(redView)]]; 

आईबी में autolayout एक दर्द हो सकता है और मैंने पाया है कि अपने आप को दृश्य स्वरूप भाषा के साथ परिचित constraintWithItem की तुलना में कम काम था:

उत्तर

6

यहाँ मैं यह कैसे करना होगा है विशेषता: relatedBy: toItem: विशेषता: गुणक: निरंतर:

+0

धन्यवाद! यह काम करता है (गायब होने के बाद वी: | [redView] ... लेकिन क्यों बिल्ली मेरा अन्य कोड काम नहीं करता ??? यह बराबर है आईबी क्या दिखाता है, है ना? – user1459524

+0

हां, मुझे वहां एक पाइप लापता था। फिक्स्ड। और, ईमानदार होने के लिए, मुझे नहीं पता। मुझे वीएफएल मिला और मेरी ऑटोलाउट जरूरतों के लिए आगे नहीं देखा। – geraldWilliam

+0

हाय दोस्तों। 6 महीने बाद मुझे एक ही समस्या का सामना करना पड़ा, लेकिन मैं टी करना चाहता हूं mycontroller.view के साथ टोपी सीधे, mycontroller.view के अंदर एक दृश्य नहीं। और यह बस करता है ... कुछ भी नहीं! – shinyuX

1

अग्रणी और पीछे की ओर, ऊपर और नीचे का उपयोग करें। ध्यान दें कि आप इसे मूल पोस्ट में मिश्रित कर रहे हैं। अग्रणी और पिछला लग रहा है लोगों को भ्रमित!

7

मुझे पता है कि यह पुराना है, लेकिन मुझे लगता है कि आपकी बाधाओं के साथ समस्या यह थी कि आप भ्रमित कर रहे थे कि पीछे की ओर क्या चल रहा है। मैं निम्नलिखित के लिए अपने कोड में संशोधन किया है और यह अब स्क्रीन के रूप में उम्मीद भरता है:

 UIView *redView = [[UIView alloc] initWithFrame:self.view.bounds]; 
redView.backgroundColor = [UIColor redColor]; 
[self.view addSubview:redView]; 

redView.translatesAutoresizingMaskIntoConstraints = NO; 

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f]]; 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f]]; 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f]]; 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f]]; 

आप अनुगामी बाधा नीचे करने के लिए बराबर (अनुगामी वास्तव में दाहिने हाथ की ओर नीचे नहीं है और आप संबंधित थे की स्थापना कर रहे थे बाएं हाथ की ओर शीर्ष बाधा (अग्रणी)

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