2017-11-23 26 views
5

मुझे UIBarButtonItem में UIButton जोड़ने के साथ एक ज्ञात समस्या है। मैंने stackoveflow में सुझाए गए ऑटो लेआउट बाधाओं को जोड़ने का प्रयास किया है, लेकिन मुझे नीचे वर्णित त्रुटि मिल रही है।आईओएस 11 - UIBarButtonItem के अंदर UIButton एक autolayout त्रुटि का कारण बनता है

UIButton *sortButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[sortButton setFrame:CGRectMake(10, 0, 100, 30)]; 
[sortButton setTitle:@"Sort" forState:UIControlStateNormal]; 
[[sortButton titleLabel] setFont:[UIFont boldSystemFontOfSize:13]]; 
[sortButton setBackgroundImage:[UIImage imageNamed:@"backgroun-button.png"] forState:UIControlStateNormal]; 
[sortButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
[sortButton addTarget:self action:@selector(sortCollection:) forControlEvents:UIControlEventTouchUpInside]; 
[sortButton applyNavBarConstraints:100 height:30]; 

[self setSortCollection:item]; 

[self setToolbarItems:[NSArray arrayWithObjects:self.sortCollection, nil]]; 

autolayout की कमी:

- (void)applyNavBarConstraints:(CGFloat)width height:(CGFloat)height 
{ 
    if (width == 0 || height == 0) { 
     return; 
    } 

    NSLayoutConstraint* w = [self.widthAnchor constraintEqualToConstant:width]; 
    NSLayoutConstraint * h = [self.heightAnchor constraintEqualToConstant:height]; 

    [w setActive:YES]; 
    [h setActive:YES]; 
} 

स्टैक ट्रेस: ​​

[LayoutConstraints] Unable to simultaneously satisfy constraints. 
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
     (1) look at each constraint and try to figure out which you don't expect; 
     (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x608000280af0 h=--& v=--& UIToolbar:0x7f85ca1327e0.width == 768 (active)>", 
    "<NSLayoutConstraint:0x6000002826c0 _UIToolbarContentView:0x7f85ca1307b0.trailing == UIToolbar:0x7f85ca1327e0.trailing (active)>", 
    "<NSLayoutConstraint:0x600000282580 H:|-(0)-[_UIToolbarContentView:0x7f85ca1307b0] (active, names: '|':UIToolbar:0x7f85ca1327e0)>", 
    "<NSLayoutConstraint:0x600000285410 H:|-(0)-[_UIButtonBarStackView:0x7f85ca135660] (active, names: '|':_UIToolbarContentView:0x7f85ca1307b0)>", 
    "<NSLayoutConstraint:0x600000285460 _UIButtonBarStackView:0x7f85ca135660.trailing == _UIToolbarContentView:0x7f85ca1307b0.trailing (active)>", 
    "<NSLayoutConstraint:0x600000285500 UIButton:0x7f85ca131b90'Sortieren'.width == 100 (active)>", 
    "<NSLayoutConstraint:0x608000280ff0 'UISV-canvas-connection' UILayoutGuide:0x6000001ba240'UIViewLayoutMarginsGuide'.leading == UIButton:0x7f85ca131b90'Sortieren'.leading (active)>", 
    "<NSLayoutConstraint:0x608000280d70 'UISV-canvas-connection' UILayoutGuide:0x6000001ba240'UIViewLayoutMarginsGuide'.trailing == UIButton:0x7f85ca131b90'Sortieren'.trailing (active)>", 
    "<NSLayoutConstraint:0x600000284f10 'UIView-leftMargin-guide-constraint' H:|-(0)-[UILayoutGuide:0x6000001ba240'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':_UIButtonBarStackView:0x7f85ca135660)>", 
    "<NSLayoutConstraint:0x600000285190 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x6000001ba240'UIViewLayoutMarginsGuide']-(0)-|(LTR) (active, names: '|':_UIButtonBarStackView:0x7f85ca135660)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600000285500 UIButton:0x7f85ca131b90'Sortieren'.width == 100 (active)> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
+0

मैं सामान्य रूप से किसी भी बाधा के बिना इस का उपयोग करें, और जोड़ने लचीला रिक्त स्थान 'UIBarButtonSystemItemFlexibleSpace' कारण है कि आप की कमी की ज़रूरत है? –

+0

आईओएस 11 अब 'UIBatButtonItem' के लिए ऑटोलाउटआउट का उपयोग करता है, यानी यह आपके _Sortieren_ बटन के लिए अग्रणी और पीछे की बाधाओं को भी सेट करता है। इस प्रकार, जैसा कि आप करते हैं, चौड़ाई की बाधा भी सेट करना अब संभव नहीं है। इस बाधा को सेट न करने का प्रयास करें और संघर्ष समाप्त होना चाहिए। –

+0

@ रेनहार्ड मैनर मुझे अपने बटन पर चौड़ाई सेट करने की ज़रूरत है अन्यथा मेरा बटन बहुत लंबा है जिसे हम उम्मीद नहीं करेंगे। – aymeba

उत्तर

1

मैं iOS11 के बाद भी इस समस्या को मिला है। मैंने इसे अपने स्वयं के XIB के साथ कस्टम व्यू बनाकर हल किया और इसमें एक बटन लगाया। फिर आप XIB में इंटरफ़ेसबिल्डर के भीतर आसानी से बाधाओं को सेट कर सकते हैं।

self.customButtonView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([CustomButtonView class]) owner:nil options:nil] objectAtIndex:0]; 
[self.customButtonView.button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside]; 
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.customButtonView]; 

बस दूसरी पंक्ति जहां आप वास्तव में बटन के लिए एक चयनकर्ता (जो एक IBOutlet है आवंटित करने के लिए है भूल नहीं है: फिर जब आप वास्तव में एक barbuttonitem में इसका उपयोग करना चाहते, कोड बहुत ही कम है पाठ्यक्रम के कस्टम दृश्य में)।

2

बार बटन बाएं और दाएं को बाध्य किया जाता है, इसलिए चौड़ाई बाधा को तोड़ना पड़ता है। आप टूलबार में लचीली जगह जोड़ कर इसे ठीक कर सकते हैं। यह आपको बटन की चौड़ाई को बाधित करने की अनुमति देता है और लचीला स्थान बाकी को भर देता है।

Obj सी:

UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 
[self setToolbarItems:[NSArray arrayWithObjects:self.sortCollection, flexItem, nil]]; 

स्विफ्ट:

let flexItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) 
toolbar.setItems([barButtonItem, flexItem], animated: false) 
1

ऑटो लेआउट के साथ बटन बनाने के लिए इस आसान तरीका का प्रयोग करें। UIView के आकार के अनुसार बटन बनाएं लचीली चौड़ाई के साथ होगा। अतिरिक्त बाधाओं को जोड़ने की कोई जरूरत नहीं है।

UIButton *sortButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [sortButton setFrame:CGRectMake(10, 0, self.view.frame.size.width - 10, 30)]; 
संबंधित मुद्दे