मैं आईओएस में ऑटो लेआउट के लिए नया हूँ। मुझे वास्तव में अवधारणा में अवधारणा पसंद है, लेकिन यह मुझे सबसे आसान काम करने की कोशिश कर रहे पागल हो रहा है। मुझे संदेह है कि मैं अभी भी कुछ सरल अंतर्निहित सिद्धांत खो रहा हूं। मैं वास्तव में एक ऐप में इसके साथ काम करने से पहले मूल बातें करने और सीखने की कोशिश कर रहा हूं, इसलिए मैं बहुत ही सरल परीक्षण परियोजनाएं बना रहा हूं। यहां एक सरल है जैसा कि यह मिलता है जो अपेक्षा के अनुसार काम नहीं करता है। पहला हिस्सा जो काम करता है। आईबी में, मैं एक दृश्य को जोड़ने के पूरे ViewController को भरने के लिए, और XCode स्वचालित रूप से/शीर्ष/ऊपर से नीचे की कमी सेट अग्रणी/ट्रेलिंग और 0 पर अंतरिक्ष जब आईबी के साथ किया, यह उम्मीद के रूप में काम करता है:आईबी में उन्हें सेट करने से प्रोग्रामेटिक रूप से अलग-अलग बाधाओं को सेट करना?
ग्रेट करने के लिए घूमता है!
अब मैं कोड में एक ही बात करने के लिए कोशिश कर रहा हूँ:
- (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]];
}
सभी कोड है कि वहाँ एक एकल दृश्य ऐप्स के लिए डिफ़ॉल्ट कोड के अलावा अन्य है।
जब मैं ऊपर कोड चलाने के लिए, एक ही बात को प्रतिबिंबित करने के रूप में मैं आईबी के साथ प्रोग्राम के रूप में मिलता है की कोशिश कर रहा है, मैं रोटेशन के बाद इस मिल:
कैसे आ है कि एक ही कमी भिन्न परिणाम मिल? शायद यह वास्तव में कुछ सरल और शर्मनाक बेवकूफ है कि मैं याद कर रहा हूं। मदद!!!
[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 की तुलना में कम काम था:
धन्यवाद! यह काम करता है (गायब होने के बाद वी: | [redView] ... लेकिन क्यों बिल्ली मेरा अन्य कोड काम नहीं करता ??? यह बराबर है आईबी क्या दिखाता है, है ना? – user1459524
हां, मुझे वहां एक पाइप लापता था। फिक्स्ड। और, ईमानदार होने के लिए, मुझे नहीं पता। मुझे वीएफएल मिला और मेरी ऑटोलाउट जरूरतों के लिए आगे नहीं देखा। – geraldWilliam
हाय दोस्तों। 6 महीने बाद मुझे एक ही समस्या का सामना करना पड़ा, लेकिन मैं टी करना चाहता हूं mycontroller.view के साथ टोपी सीधे, mycontroller.view के अंदर एक दृश्य नहीं। और यह बस करता है ... कुछ भी नहीं! – shinyuX