繁体   English   中英

存在 UIToolbarContentView 时无法同时满足约束

[英]Unable to simultaneously satisfy constraints when UIToolbarContentView is present

我在日期选择器视图顶部有一个自定义按钮。 当日期选择器处于活动状态时,我得到“无法同时满足约束”。 在我更新到 Xcode 11 后,错误已弹出,

这是我的代码:

let toolbar = UIToolbar()
let flexBarButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
let done = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(viewTapped))
toolbar.sizeToFit()
toolbar.setItems([flexBarButton,done], animated: false)
dateInputTextField.inputAccessoryView = toolbar

这是错误:

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:0x7f85a6fe4230 h=--& v=--& _UIToolbarContentView:0x7f85a6d1b8a0.width == 0   (active)>",
    "<NSLayoutConstraint:0x7f85a6c2c220 H:|-(0)-[_UIButtonBarStackView:0x7f85a6d1c0c0]   (active, names: '|':_UIToolbarContentView:0x7f85a6d1b8a0 )>",
    "<NSLayoutConstraint:0x7f85a6c2e5e0 _UIButtonBarStackView:0x7f85a6d1c0c0.trailing == _UIToolbarContentView:0x7f85a6d1b8a0.trailing   (active)>",
    "<NSLayoutConstraint:0x7f85a6c2f920 'TB_Leading_Leading' H:|-(16)-[_UIModernBarButton:0x7f85a6edf790'Done']   (active, names: '|':_UIButtonBarButton:0x7f85a6ed7060 )>",
    "<NSLayoutConstraint:0x7f85a6c2d690 'TB_Trailing_Trailing' H:[_UIModernBarButton:0x7f85a6edf790'Done']-(16)-|   (active, names: '|':_UIButtonBarButton:0x7f85a6ed7060 )>",
    "<NSLayoutConstraint:0x7f85a6f11060 'UISV-canvas-connection' UILayoutGuide:0x7f85a6c22b40'UIViewLayoutMarginsGuide'.leading == UIView:0x7f85a6ed6cd0.leading   (active)>",
    "<NSLayoutConstraint:0x7f85a6f25390 'UISV-canvas-connection' UILayoutGuide:0x7f85a6c22b40'UIViewLayoutMarginsGuide'.trailing == _UIButtonBarButton:0x7f85a6ed7060.trailing   (active)>",
    "<NSLayoutConstraint:0x7f85a6f256e0 'UISV-spacing' H:[UIView:0x7f85a6ed6cd0]-(0)-[_UIButtonBarButton:0x7f85a6ed7060]   (active)>",
    "<NSLayoutConstraint:0x7f85a6c24880 'UIView-leftMargin-guide-constraint' H:|-(0)-[UILayoutGuide:0x7f85a6c22b40'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':_UIButtonBarStackView:0x7f85a6d1c0c0 )>",
    "<NSLayoutConstraint:0x7f85a6c2c9c0 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x7f85a6c22b40'UIViewLayoutMarginsGuide']-(0)-|(LTR)   (active, names: '|':_UIButtonBarStackView:0x7f85a6d1c0c0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7f85a6c2d690 'TB_Trailing_Trailing' H:[_UIModernBarButton:0x7f85a6edf790'Done']-(16)-|   (active, names: '|':_UIButtonBarButton:0x7f85a6ed7060 )>

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.

有什么想法可以解决这个错误吗?

在我的情况下,这是通过在实例化 UIToolbar 时指定一个有效的框架来解决的。
UIToolbar(frame: ..)而不仅仅是UIToolbar()

例如:

UIToolbar(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: _viewSizeWidth, height: CGFloat(44))))

其中 _viewSizeWidth 是根据 window 大小在其他地方计算的。

Swift 5.2

UIToolbar的宽度设置为屏幕宽度时,似乎会发生约束错误。 高度似乎无关紧要。

我通过设置足够宽以接受UIBarButtonItem (s) 来消除警告。 使宽度太小也会引发错误。

let toolBar = UIToolbar(frame: CGRect(origin: .zero, size: CGSize(width: 100, height: 44.0)))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM