UIToolbar与UIBarButtonItem LayoutConstraint问题 [英] UIToolbar with UIBarButtonItem LayoutConstraint issue

查看:84
本文介绍了UIToolbar与UIBarButtonItem LayoutConstraint问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以编程方式创建带有UIBarButtonItem的UIToolbar.我没有为此ViewController使用.xib或Storyboard.这是我如何创建它的代码.

I'm creating a UIToolbar with UIBarButtonItem in it programatically. I'm not using .xib or storyboard for this ViewController. Here's the code of how I created it.

NSMutableArray *items = [[NSMutableArray alloc] init];

UIBarButtonItem *buttonItem;
buttonItem = [[UIBarButtonItem alloc ] initWithTitle: [Language get:@"Home" alter:nil]
                                               style: UIBarButtonItemStyleBordered
                                              target: self
                                              action: @selector(viewChangeTo)];
[items addObject:buttonItem];

toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 0, 1024, 44);
[toolbar setItems:items animated:NO];
[self.view addSubview:toolbar];

每次加载视图时,都会出现LayoutConstraints错误.

Each time the view get loaded I'm getting the LayoutConstraints error.

[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. 

"<NSLayoutConstraint:0x6000023de350 H:|-(20)-[_UIModernBarButton:0x7fed8c463e00'Home']   (active, names: '|':_UIButtonBarButton:0x7fed8c463bb0 )>",
"<NSLayoutConstraint:0x6000023de3a0 H:[_UIModernBarButton:0x7fed8c463e00'Home']-(20)-|   (active, names: '|':_UIButtonBarButton:0x7fed8c463bb0 )>",
"<NSLayoutConstraint:0x6000023dff70 '_UITemporaryLayoutWidth' _UIButtonBarButton:0x7fed8c463bb0.width == 0   (active)>

Will attempt to recover by breaking constraint 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2019-01-21 13:14:24.777354+0800 M[50994:10130357] [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. 
(
    "<NSLayoutConstraint:0x6000023de530 _UIModernBarButton:0x7fed8c463e00'Home'.lastBaseline == UILayoutGuide:0x6000039eef40'UIViewLayoutMarginsGuide'.bottom   (active)>",
    "<NSLayoutConstraint:0x6000023de580 V:|-(>=0)-[_UIModernBarButton:0x7fed8c463e00'Home']   (active, names: '|':_UIButtonBarButton:0x7fed8c463bb0 )>",
    "<NSLayoutConstraint:0x6000023e5680 UIButtonLabel:0x7fed8c457e90'Home'.centerY == _UIModernBarButton:0x7fed8c463e00'Home'.centerY + 1.5   (active)>",
    "<NSLayoutConstraint:0x6000023e4c80 '_UITemporaryLayoutHeight' _UIButtonBarButton:0x7fed8c463bb0.height == 0   (active)>",
    "<NSLayoutConstraint:0x6000023de490 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x6000039eef40'UIViewLayoutMarginsGuide']-(16)-|   (active, names: '|':_UIButtonBarButton:0x7fed8c463bb0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000023e5680 UIButtonLabel:0x7fed8c457e90'Home'.centerY == _UIModernBarButton:0x7fed8c463e00'Home'.centerY + 1.5   (active)>

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

推荐答案

使用swift而不是: let toolbar = UIToolbar()

using swift instead of: let toolbar = UIToolbar()

使用: let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 35))

这篇关于UIToolbar与UIBarButtonItem LayoutConstraint问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆