2012-12-13 20 views
9

के साथ एक 'अमान्य जोड़ी' क्यों है, मैं सुपर व्यू की चौड़ाई के आधार पर ऑटो लेआउट प्रबंधक दृश्य के केंद्र बिंदु को समायोजित करने का प्रयास कर रहा हूं। मुझे समझ नहीं आता क्यों कि विशेषताओं के 'अमान्य युग्मित कर रहा है' हैNSLayoutAttributeCenterX NSLayoutAttributeWidth

UIView *ac; 
NSLayoutConstraint *cXloc = [NSLayoutConstraint constraintWithItem:ac 
                 attribute:NSLayoutAttributeCenterX 
                 relatedBy:NSLayoutRelationEqual 
                  toItem:ac.superview 
                 attribute:NSLayoutAttributeWidth 
                 multiplier:.1 
                  constant:x*ac.superview.frame.size.width*.2]; 
[ac.superview addConstraint:cXloc]; 

कोई समझा सकते हैं क्यों यह 'अमान्य कतरन' है और मैं यह कैसे संपर्क करना चाहिए (के रूप में दुर्घटना और NSInvalidArgumentException ने बताया)? धन्यवाद

+2

वास्तव में अच्छा सवाल है। रहस्यमय लगता है। –

उत्तर

3

यदि आप एसी के एट्रिब्यूट सेंटररएक्स को अपने पर्यवेक्षक के एट्रिब्यूट सेंटर, एक्सट्रिब्यूटिंग, या एट्रिब्यूट ट्रायलिंग से संबंधित करते हैं, तो आप गुणक और बाधा का उपयोग करके अपनी वांछित बाधा व्यक्त करने में सक्षम होना चाहिए। ध्यान रखें कि निरंतर मूल्यांकन किया जाता है जब बाधा उत्पन्न होती है, और आपका उदाहरण स्थिरांक AC.superview की चौड़ाई में परिवर्तन के रूप में अपडेट नहीं होगा।

यदि आप शब्दों में व्यक्त कर सकते हैं कि आप अपने पर्यवेक्षण के सापेक्ष एसी कैसे चाहते हैं, तो हम एक बाधा का सुझाव दे सकते हैं।

संपादित

यहाँ 5 NSButtons साथ एक उदाहरण है। वे स्वयं और उनके बीच की जगह का विस्तार होता है ताकि रिक्त स्थान बटन के रूप में 30% चौड़े हों, सभी बटनों की समान चौड़ाई होती है, और सभी रिक्त स्थानों की समान चौड़ाई होती है। केवल अंतर के लिए 4 अदृश्य NSViews बनाना बहुत बोझिल है, विशेष रूप से यह समझते हुए कि आप इसे ऑटोलायआउट के बाहर काम कर चुके हैं। लेकिन मामले में आप उत्सुक हैं:

// Assuming these NSViews and NSButtons exist, 
//NSView* superview ; 
//NSButton *buttonOne, *buttonTwo, *buttonThree, *buttonFour, *buttonFive ; 


[superView removeConstraints:superView.constraints] ; 

// Create empty NSViews to fill the space between the 5 buttons. 
NSView* spaceOne = [NSView new] ; 
NSView* spaceTwo = [NSView new] ; 
NSView* spaceThree = [NSView new] ; 
NSView* spaceFour = [NSView new] ; 
spaceOne.translatesAutoresizingMaskIntoConstraints = NO ; 
spaceTwo.translatesAutoresizingMaskIntoConstraints = NO ; 
spaceThree.translatesAutoresizingMaskIntoConstraints = NO ; 
spaceFour.translatesAutoresizingMaskIntoConstraints = NO ; 
[superView addSubview:spaceOne] ; 
[superView addSubview:spaceTwo] ; 
[superView addSubview:spaceThree] ; 
[superView addSubview:spaceFour] ; 

NSDictionary* views = NSDictionaryOfVariableBindings(superView,buttonOne,buttonTwo,buttonThree,buttonFour,buttonFive,spaceOne,spaceTwo,spaceThree,spaceFour) ; 

// Vertically align buttonOne to its superview however you like. 
[superView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[buttonOne]" options:0 metrics:nil views:views ] ] ; 

// Make the "space" NSViews' widths equal and >= 10. Make the buttons' widths equal. 
[superView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[buttonOne][spaceOne(>=10)][buttonTwo(==buttonOne)][spaceTwo(==spaceOne)][buttonThree(==buttonOne)][spaceThree(==spaceOne)][buttonFour(==buttonOne)][spaceFour(==spaceOne)][buttonFive(==buttonOne)]|" options: NSLayoutFormatAlignAllCenterY metrics:nil views:views ] ] ; 
// Make the "space" NSViews' widths 30% of the NSButtons' widths. 
[superView addConstraint: [NSLayoutConstraint constraintWithItem: spaceOne 
                 attribute: NSLayoutAttributeWidth 
                 relatedBy: NSLayoutRelationEqual 
                  toItem: buttonOne 
                 attribute: NSLayoutAttributeWidth 
                 multiplier: 0.3 
                 constant: 0 ] ] ; 
+0

मेरा लक्ष्य एक पर्यवेक्षण में क्षैतिज रूप से 5 आइटम लेआउट करना है जो उनके बीच समान दूरी के साथ है। ऑब्जेक्ट की चौड़ाई पर्यवेक्षण की चौड़ाई के साथ बदलनी चाहिए। सबव्यू के पहलू अनुपात को बाधित किया जाता है, इसलिए यह संभावित अंतराल स्थान छोड़ देता है जिसे मैं सभी बराबर होना चाहता हूं। मैं अपने जीवन के लिए यह नहीं समझ सकता कि इसे कैसे व्यक्त किया जाए। इसके अतिरिक्त, मुझे समझ में नहीं आता क्यों केंद्रएक्स चौड़ाई से संबंधित नहीं हो सकता है? अंत में, समीकरण की स्थैतिक प्रकृति मूल रूप से अवांछित है।मैं समीकरण या आलसी गणना की अभिव्यक्ति क्यों निर्दिष्ट नहीं कर सकता? – MobileVet

+0

तो, 5 आइटम की चौड़ाई पर्यवेक्षण के साथ बदलनी चाहिए। क्या इसका मतलब है कि उनके बीच बराबर अंतर पर्यवेक्षण की चौड़ाई में परिवर्तन के समान ही रहेगा? –

+0

काफी नहीं, सबकुछ बढ़ना चाहिए/तदनुसार घटाना चाहिए। अगर मैं सुपर व्यू को छोटा करता हूं, तो प्रत्येक सबव्यू को भी कम करना चाहिए। नतीजतन, यह दृश्यों के बीच में अंतर को समायोजित करेगा। इनमें से कोई भी मूल्य तय नहीं है, वे सभी गतिशील होना चाहिए। मैंने इसे तोड़ दिया है और इसे मैन्युअल रूप से किया है, जो वास्तव में काफी आसान था। मैं अभी भी जानना चाहता हूं कि कैसे/अगर यह बाधाओं के साथ पूरा किया जा सकता है। – MobileVet

4

यह Auto Layout का वर्तमान क्रियान्वयन की सीमा है। हालांकि, आप आसानी से इसके आसपास काम कर सकते हैं क्योंकि सभी बाधाएं रैखिक हैं और NSLayoutAttribute एस सहसंबंधित हैं। उदाहरण के लिए, मान लीजिए बाधा आप चाहते है:

subview.centerX = m * superview.width + c; 

आप इसे के बीच टो centerX रों एक रिश्ते के रूप में व्यक्त कर सकते हैं:

// Since width == 2 * centerX 
subview.centerX = m * 2 * superview.centerX + c; 
+1

आप मान रहे हैं कि 'superview.width'' 2 * superview.centerX' के बराबर है। यदि 'superview.frame.origin.x = 0' है तो क्या यह सच नहीं होगा _only_? सेंटरएक्स अपने माता-पिता में दृश्य की स्थिति से प्रभावित है। –

+0

@ an0: 'superview.centerX = superview.leftXX + superview.width/2' इसलिए यह काफी काम नहीं करता है। @ ग्लेन-स्किमिट सही है – wcochran

1

AN0 के जवाब के आधार पर, और यह सोचते हैं कि आप एक NSArray युक्त है आपके बटन, अंतरिक्ष चाहिए बटन समान रूप से superview भीतर निम्नलिखित:

NSUInteger currentButton = 1; 
    for (UIButton *button in self.buttons) 
    { 
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:2.0 * (CGFloat) currentButton/(CGFloat) (self.buttons.count + 1) constant:0.0]]; 
    currentButton++; 
    } 
0

आप अपने प्रोग्राम के रूप में उत्पन्न अपने superview के वाई फिट करने के लिए विचारों के लिए देख रहे हैं डीटीएच। आप NSLayoutAttributeLeading और attribute:NSLayoutAttributeCenterX

की बाधा जोड़ी का उपयोग कर सकते हैं आपको सही गुणक प्राप्त करने के लिए उचित गणना करना है। गणना में विचारों की कुल गणना और वर्तमान दृश्य की अनुक्रमणिका शामिल है।

//Caculate constraint multiplier from parentView CenterX. 
    //This sets the width of the button relative to parentView. 
    // A value of 2 = Full width. 

    CGFloat multiplier = 2/(arr.count/counter); 

    [parentView addConstraint:[NSLayoutConstraint constraintWithItem:btn 
        attribute:NSLayoutAttributeTrailing 
        relatedBy:NSLayoutRelationEqual toItem:parentView 
        attribute:NSLayoutAttributeCenterX multiplier:multiplier constant:0]]; 

यह दृश्य पर्यवेक्षण को भरने के लिए दृश्य चौड़ाई वितरित करेगा।