2017-11-23 11 views
5

UIButtonをUIBarButtonItemに追加する際の既知の問題がありました。 stackoveflowで提案されている自動レイアウト制約を追加しようとしましたが、以下のようなエラーが表示されます。ios 11 - UIBarButtonItem内のUIButtonによって自動レイアウトエラーが発生する

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

自動レイアウトの制約:

- (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

iOS11は 'UIBatButtonItem'にautolayoutを使用します。つまり、_Sortieren_ボタンの先頭と末尾の制約も設定します。このように、幅の制約を設定することはもはや不可能です。この制約を設定しないと、競合はなくなります。 –

+0

@ReinhardMänner私のボタンには幅を設定する必要があります。 – aymeba

答えて

1

私はiOS11以降も、この問題を持っていました。私はそれを自分のXIBでカスタムビューを作成し、ボタンを配置することでそれを解決しました。次に、XIBのInterfaceBuilder内で制約を簡単に設定できます。あなたが実際にbarbuttonitemでそれを使用したいとき はその後、コードは非常に短いです:

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であるあなたが実際にボタンのセレクターを割り当てる必要が二行目を(忘れないでくださいもちろんカスタムビューで)。

2

バーのボタンが左右に拘束されているため、幅の制約を解除する必要があります。これを修正するには、ツールバーに柔軟なスペースを追加します。これにより、ボタンの幅を制限し、残りの部分を柔軟なスペースで埋めることができます。

OBJのC:

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)]; 
関連する問題