存在 UIToolbarContentView 时无法同时满足约束
Unable to simultaneously satisfy constraints when UIToolbarContentView is present
我在日期选择器视图顶部有一个自定义按钮。当日期选择器处于活动状态时,我得到 "Unable to simultaneously satisfy constraints." 更新到 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)))
我在日期选择器视图顶部有一个自定义按钮。当日期选择器处于活动状态时,我得到 "Unable to simultaneously satisfy constraints." 更新到 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)))