2013-03-12 7 views
16

मेरे पास और buttonsuperView में इस तरह है।लेआउट बाधाओं को प्राथमिकता जोड़ना

|--------------[Label]-----[button]-| 

मैं चाहता labelcentred होना संभव है, तो button करने के लिए एक मिनट के अंतराल है और बाईं ओर ले जाएँ।

तो अगर बटन बड़ा है यह कैसा दिखता ...

|-[  LABEL!  ]-[button]-| 

तो, बटन रहता है कि वह कहाँ है और एक ही आकार में। और तत्वों के बीच न्यूनतम अंतराल हैं।

मैं centerX बाधा जोड़ सकता हूं लेकिन मुझे इसे प्राथमिकता नहीं दे रही है इसलिए यह Required बनी हुई है।

मैं इस स्थिति को कैसे बना सकता हूं? मैं कोड में सभी ऑटो लेआउट कर रहा हूँ।

की कमी मैं वर्तमान में कर रहे हैं है ...

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[_label]-(>[email protected])-[_button(==45)]-|" 
                  options:NSLayoutFormatAlignAllCenterY 
                  metrics:nil 
                   views:views]]; 

[self addConstraint:[NSLayoutConstraint constraintWithItem:_label 
               attribute:NSLayoutAttributeCenterX 
               relatedBy:NSLayoutRelationEqual 
                toItem:self.contentView 
               attribute:NSLayoutAttributeCenterX 
               multiplier:1.0 
                constant:0.0]]; 

लेकिन मुझे यकीन है कि कैसे दूसरे बाधा की प्राथमिकता कम करने के लिए नहीं कर रहा हूँ।

उत्तर

35

तुम बस इतना की तरह, बाधा के priority गुण सेट:

NSLayoutConstraint *centeringConstraint = 
    [NSLayoutConstraint constraintWithItem:_label 
           attribute:NSLayoutAttributeCenterX 
           relatedBy:NSLayoutRelationEqual 
            toItem:self.contentView 
           attribute:NSLayoutAttributeCenterX 
           multiplier:1.0 
            constant:0.0]; 

centeringConstraint.priority = 800; // <-- this line 

[self addConstraint:centeringConstraint]; 
बेशक
+0

! धन्यवाद! मैंने इसे जोड़ने से पहले ऑब्जेक्ट बनाने के बारे में नहीं सोचा था। उत्तम! इसे अब हल किया गया, धन्यवाद! – Fogmeister

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