存在 UIToolbarContentView 时无法同时满足约束
Posted
技术标签:
【中文标题】存在 UIToolbarContentView 时无法同时满足约束【英文标题】:Unable to simultaneously satisfy constraints when UIToolbarContentView is present 【发布时间】:2019-10-23 20:16:36 【问题描述】:我在日期选择器视图顶部有一个自定义按钮。当日期选择器处于活动状态时,我得到“无法同时满足约束”。在我更新到 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 在第一个按钮上设置的内部约束,并且它似乎正确显示,所以我现在忽略日志警告。 【参考方案1】:在我的例子中,这是通过在实例化 UIToolbar 时指定一个有效的框架来解决的。UIToolbar(frame: ..)
而不仅仅是UIToolbar()
例如:
UIToolbar(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: _viewSizeWidth, height: CGFloat(44))))
其中 _viewSizeWidth 是根据窗口大小在别处计算得出的。
【讨论】:
我无法让它工作。你设置了什么尺寸? 谢谢@jk7。在对主视图进行一些调整后,我使用了UIToolbar(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: self.view.bounds.width, height: CGFloat(44))))
,对于自定义表格视图单元格中的文本字段,我使用了UIToolbar(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: UIScreen.main.bounds.width, height: CGFloat(44))))
。
简单地使用 100 和 100 也可以(来自forums.developer.apple.com/thread/121474#384249)
这也解决了我的冲突约束。这是 Xamarin 版本的代码: var dismissKeyboard = new UIToolbar (new CGRect (0.0f, 0.0f, this.vwPickerContainer.Frame.Width, 50.0f));【参考方案2】:
Swift 5.2
UIToolbar
的宽度设置为屏幕宽度时似乎出现约束错误。高度似乎无关紧要。
我通过设置足够宽以接受UIBarButtonItem
(s) 来消除警告。将宽度设置得太小也会引发错误。
let toolBar = UIToolbar(frame: CGRect(origin: .zero, size: CGSize(width: 100, height: 44.0)))
【讨论】:
可以确认,也帮我修好了。以上是关于存在 UIToolbarContentView 时无法同时满足约束的主要内容,如果未能解决你的问题,请参考以下文章
Oracle ORA-00942:表或视图存在时不存在 [重复]