2014-10-02 13 views
10

पर पिन मैं एक यूआई तत्व है (वास्तव में UISwitch, लेकिन कोई फ़र्क नहीं पड़ता) दोनों प्रमुख और अनुगामी इंटरफ़ेस बिल्डर में superview में पिन किए गए अंतरिक्ष है। बाधा Xcode 6 में इस तरह दिखता है:iOS8 ऑटो लेआउट प्रोग्राम के रिश्तेदार लेआउट मार्जिन

Constraint pin

प्रमुख अंतरिक्ष के लिए बाधा ही प्रभावी ढंग से है। बाधा का मूल्य 42.0 अंक है।

यह वह जगह है मैं वास्तव में क्या चाहते हैं, क्योंकि विभिन्न उपकरणों के लिए मैं, UIView पर layoutMargins संपत्ति को बदल सकते हैं और बाधाओं को सही ढंग से काम करेंगे विचारों के बीच मार्जिन को बढ़ाने के।

अब मैं कोड में एक और दृश्य जोड़ना चाहता हूं जिसमें इसके पर्यवेक्षण मार्जिन पर पिन किए गए अग्रणी और पिछली जगह दोनों होंगी, इसलिए उसी layoutMargins पर्यवेक्षण पर सेट होगा।

मैं दृश्य प्रारूप भाषा का उपयोग कर निम्न सिंटैक्स के साथ दृश्य पिन किए गए:

NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-42.0-[separatorView]-42.0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(self.contentView, separatorView)]; 

[self.contentView addConstraints:constraints]; 
[self.contentView setNeedsUpdateConstraints]; 

यह काम करता है, लेकिन layoutMargins संपत्ति तो यह स्पष्ट रूप से मार्जिन पर पिन किया गया नहीं है, इस बाधा का उपयोग कर कोई प्रभाव नहीं है, लेकिन सीधे पर्यवेक्षण के लिए।

तो मेरे सवाल है:

कैसे दृश्य प्रारूप भाषा का प्रयोग कर कोड में मार्जिन के लिए यूआई तत्व रिक्त स्थान पिन करने के लिए? या यदि संभव नहीं है, तो constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant: एपीआई के साथ पिन कैसे करें?

धन्यवाद! या - | ""

उत्तर

14

iOS8 में, दृश्य प्रारूप भाषा ताकि अद्यतन किया गया है "- |" पर्यवेक्षी के लेआउट मार्जिन प्रॉपर्टी द्वारा परिभाषित एक अंतर का उपयोग करने के लिए डिफ़ॉल्ट होगा।

// programmatically set the layoutMargins, only if 
// you want non-default values and they are not already set in IB! 
self.contentView.layoutMargins = UIEdgeInsetsMake(0,42,0,42); // set left and right margins to 42 

// assume: seperatorView is already a subview of self.contentView 

// separatorView will use the constraints because we write "-" between it and the superview edge 
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[separatorView]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(separatorView)]; 
[self.contentView addConstraints:constraints]; 

आप जब सीधे एपीआई के माध्यम से बाधाओं का निर्माण, तो आप नए iOS8 केवल लेआउट विशेषताओं का उपयोग लेआउट मार्जिन का उल्लेख करना चाहते हैं:

तो दृश्य प्रारूप भाषा का प्रयोग कर जवाब इस प्रकार है:

NSMutableArray * constraints = [NSMutableArray array]; 
[constraints addObject:[NSLayoutConstraint constraintWithItem:self.contentView 
    attribute:NSLayoutAttributeLeftMargin 
    relatedBy:NSLayoutRelationEqual 
    toItem:separatorView 
    attribute:NSLayoutAttributeLeft 
    multiplier:1.0 
    constant:0]]; 
[constraints addObject:[NSLayoutConstraint constraintWithItem:self.contentView 
    attribute:NSLayoutAttributeRightMargin 
    relatedBy:NSLayoutRelationEqual 
    toItem:separatorView 
    attribute:NSLayoutAttributeRight 
    multiplier:1.0 
    constant:0]]; 
[self.contentView addConstraints:constraints]; 
4

मैं सहमत हूं "iOS8 में, दृश्य प्रारूप भाषा अद्यतन किया गया है, ताकि" | - "या" - | "। एक रिक्ति superview के layoutMargins संपत्ति द्वारा परिभाषित का उपयोग कर लागू हो जाएगी"

तो, आप विकल्प "मार्जिन तक रोकें" जब आप इंटरफ़ेस बिल्डर का उपयोग अपने लेआउट की सहायता के लिए टिकटिक चाहिए। यदि ऐसा है, तो यह काम करता है।

समस्या अभी भी हल नहीं किया गया था, तो आप मुझे एक डेमो परियोजना दे सकता है?

जोड़ा गया: टी his article whcih हमें नए आईओएस 8 एपीआई संरक्षित करता है SuperviewLayoutMargins, यह अधिक सहायक होने की इच्छा है।

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