2013-07-04 5 views
13

मैं अपने बच्चे को शीर्ष दृश्य से 25% आकार के दृश्य को देखने में सक्षम होना चाहता हूं।व्यू आकार के सापेक्ष एक बच्चे के दृश्य को कैसे स्थिति में रखें?

'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: Invalid pairing of layout attributes' 

त्रुटि क्यों होती है और मैं कैसे प्राप्त कर सकते हैं जो मैं चाहता:

NSLayoutConstraint *topPositionConstraint = [NSLayoutConstraint constraintWithItem:_containerView 
                      attribute:NSLayoutAttributeTop 
                      relatedBy:NSLayoutRelationEqual 
                      toItem:_childView 
                      attribute:NSLayoutAttributeHeight 
                     multiplier:0.25f 
                      constant:0.0f]; 

हालांकि, अभी मैं निम्नलिखित अपवाद हो रही है?

उत्तर

6

आप एक ही बाधा में शीर्ष और ऊंचाई का उपयोग नहीं कर सकते हैं। हालांकि यह कहना समझ में आता है कि सिस्टम इसे पसंद नहीं करता है।

क्या आप के बजाय कर सकता है कुछ की तरह है ...

NSLayoutConstraint *topPositionConstraint = 
    [NSLayoutConstraint constraintWithItem:_childView 
            attribute:NSLayoutAttributeTop 
            relatedBy:NSLayoutRelationEqual 
            toItem:_containerView 
            attribute:NSLayoutAttributeTop 
           multiplier:1.0 
            constant:_containerView.frame.size.height * 0.25]; 
+0

यह महान काम किया के बाद मैं बच्चे और कंटेनर विचारों बदली: यह भी अच्छा है क्योंकि आपके कंटेनर को देखने के फ्रेम जब तुम बाधा बना रहे हैं नहीं किया जा सकता है। – circuitlego

+0

क्षमा करें, हाँ, आप सही हैं। आप बच्चे के दृश्य पर बाधा स्थापित कर रहे हैं, इसलिए यह पहले होना चाहिए। – Fogmeister

+1

बस ध्यान दें कि "_containerView.frame.size.height" परिदृश्य और पोर्ट्रेट मोड में भिन्न होगा। इसलिए, यदि लैंडस्केप मोड से शुरू होता है तो गणना मूल्य पोर्ट्रेट मोड से शुरू होने पर समान नहीं होगा। – Vanja

22

इसके बजाय स्वीकार किए जाते हैं जवाब में के रूप में फ्रेम का उपयोग करने का, आप गुणक प्रतिशत यदि आप ऊंचाई के बजाय नीचे का उपयोग कर सकते हैं। मैं इस तकनीक का उपयोग बाल विचारों की प्रतिशत आधारित स्थिति के लिए कर रहा हूं।

NSLayoutConstraint *topPositionConstraint = 
    [NSLayoutConstraint constraintWithItem:_childView 
           attribute:NSLayoutAttributeTop 
           relatedBy:NSLayoutRelationEqual 
           toItem:_containerView 
           attribute:NSLayoutAttributeBottom 
          multiplier:0.25 
           constant:0]; 
+0

क्या यह सही है जब कंटेनर्व के पास 0,0 पर फ्रेम फ्रेम नहीं है? –

+3

यह सही है। यह माना जाता है कि उन्होंने NSLayoutAttributeTop और NSLayoutAttributeHeight को एक दूसरे के साथ संगत होने की अनुमति नहीं दी है (वे केवल संख्याएं हैं !!) और यहां तक ​​कि इसे कहीं भी दस्तावेज़ों में भी नहीं माना जाता है (जब तक कि मुझे कुछ याद नहीं आया)। इस समाधान के लिए धन्यवाद। –

+0

बहुत बढ़िया, यह स्वीकार्य उत्तर होना चाहिए, कोई फ्रेम tweking – Max

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

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