2017-11-23 17 views
5

Tôi đã biết vấn đề với việc thêm UIButton vào UIBarButtonItem. Tôi đã cố gắng để thêm các ràng buộc bố trí tự động như đề xuất trong stackoveflow nhưng tôi nhận được một lỗi được mô tả như dưới đây.ios 11 - UIButton bên trong UIBarButtonItem gây ra lỗi tự động trả lời

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]]; 

chế 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]; 
} 

Stack trace:

[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

Tôi thường sử dụng tính năng này mà không có bất kỳ ràng buộc nào và thêm không gian linh hoạt 'UIBarButtonSystemItemFlexibleSpace' tại sao bạn cần hạn chế? –

+0

iOS11 sử dụng tính năng tự động hoàn thành cho 'UIBatButtonItem', tức là nó cũng đặt ra ràng buộc hàng đầu và cuối cho nút _Sortieren_ của bạn. Do đó, bạn không còn có thể đặt giới hạn chiều rộng như bạn làm. Cố gắng không đặt ràng buộc này và xung đột sẽ biến mất. –

+0

@ ReinhardMänner Tôi cần đặt chiều rộng cho nút của mình nếu không nút của tôi quá dài mà chúng tôi không mong đợi. – aymeba

Trả lời

1

Tôi đã có vấn đề này cũng kể từ iOS11. Tôi đã giải quyết nó bằng cách tạo một chế độ xem tùy chỉnh với XIB của chính nó và đặt một nút trong đó. Sau đó, bạn có thể dễ dàng thiết lập các ràng buộc trong InterfaceBuilder trong XIB. Sau đó, khi bạn thực sự muốn sử dụng nó trong một barbuttonitem, mã này là rất ngắn:

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]; 

Chỉ cần đừng quên dòng thứ hai mà bạn thực sự phải gán một selector cho nút (mà là một IBOutlet trong chế độ xem tùy chỉnh của khóa học).

2

Nút thanh bị giới hạn ở bên trái và bên phải, do đó, giới hạn chiều rộng phải bị hỏng. Bạn có thể sửa lỗi này bằng cách thêm không gian linh hoạt vào thanh công cụ. Điều này cho phép bạn chống lại chiều rộng của nút và có không gian linh hoạt lấp đầy phần còn lại.

Obj C:

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

Swift:

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

Sử dụng cách này đơn giản để tạo nút với bố trí tự động. Nút tạo sẽ có chiều rộng linh hoạt theo kích thước của UIView. Không cần phải thêm các ràng buộc thêm.

UIButton *sortButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [sortButton setFrame:CGRectMake(10, 0, self.view.frame.size.width - 10, 30)]; 
Các vấn đề liên quan