微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么 UIDatePicker 和静态 UITableView 会出现约束错误?

如何解决为什么 UIDatePicker 和静态 UITableView 会出现约束错误?

我正在构建一个用于在应用程序中输入数据的表单,并且我正在使用仅使用静态单元格构建的 UITableView,因为用户需要填写的字段不会改变。这些字段之一是日期。对于此字段,我尝试使用设置为 UIDatePicker.compact,因此它将日期显示在一行中,当用户点击日期时,将显示模态日历(新的 iOS 14行为)。

问题是每次我点击日期(在模拟器中)时,我都会收到下面详细说明的约束错误

[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. 
(
    "<_UISystemBaselineConstraint:0x60000343e3a0 H:[_UIDatePickerLinkedLabel:0x7fb3a0d79f10]-(NSLayoutAnchorConstraintSpace(8))-[UILayoutGuide:0x600002e207e0'']   (active)>","<_UISystemBaselineConstraint:0x60000343e8a0 H:[UILayoutGuide:0x600002e207e0'']-(>=NSLayoutAnchorConstraintSpace(8))-[_UIDatePickerTouchOutsetButton:0x7fb3a0d7acc0]   (active)>","<NSLayoutConstraint:0x6000034373e0 UILabel:0x7fb3a0d7a0d0.trailing <= _UIDatePickerLinkedLabel:0x7fb3a0d79f10.trailing   (active)>","<NSLayoutConstraint:0x6000034372a0 H:|-(0)-[UILabel:0x7fb3a0d7a0d0]   (active,names: '|':_UIDatePickerLinkedLabel:0x7fb3a0d79f10 )>","<NSLayoutConstraint:0x60000343e210 _UIDatePickerLinkedLabel:0x7fb3a0d79f10.leading == UILayoutGuide:0x600002e208c0'UIViewLayoutMarginsGuide'.leading   (active)>","<NSLayoutConstraint:0x60000343e2b0 H:[_UIDatePickerTouchOutsetButton:0x7fb3a0d7acc0]-(28)-[_UIDatePickerTouchOutsetButton:0x7fb3a0d7afc0]   (active)>","<NSLayoutConstraint:0x60000343e120 _UIDatePickerTouchOutsetButton:0x7fb3a0d7afc0.trailing == UILayoutGuide:0x600002e208c0'UIViewLayoutMarginsGuide'.trailing   (active)>","<NSLayoutConstraint:0x600003441fe0 UILayoutGuide:0x600002e207e0''.width == UIImageView:0x7fb3a0d7aaf0.width   (active)>","<NSLayoutConstraint:0x600003429950 _UIDatePickerCalendarContentStackView:0x7fb3a0d76c60.width <= _UIDatePickerCalendarView:0x7fb3a0d9e2a0.width   (active)>","<NSLayoutConstraint:0x600003449f40 H:|-(0)-[_UIDatePickerCalendarView:0x7fb3a0d9e2a0]   (active,names: '|':UIDatePicker:0x7fb3a0d23900 )>","<NSLayoutConstraint:0x600003449ef0 _UIDatePickerCalendarView:0x7fb3a0d9e2a0.trailing == UIDatePicker:0x7fb3a0d23900.trailing   (active)>","<NSLayoutConstraint:0x600003420190 H:|-(8)-[UIDatePicker:0x7fb3a0d23900]   (active,names: '|':UIView:0x7fb3a0e78db0 )>","<NSLayoutConstraint:0x6000034201e0 UIDatePicker:0x7fb3a0d23900.trailing == UIView:0x7fb3a0e78db0.trailing - 8   (active)>","<NSLayoutConstraint:0x600003425ea0 'UISV-canvas-connection' _UIDatePickerCalendarContentStackView:0x7fb3a0d76c60.leading == _UIDatePickerCalendarHeaderView:0x7fb3a0d76df0.leading   (active)>","<NSLayoutConstraint:0x600003425f40 'UISV-canvas-connection' H:[_UIDatePickerCalendarHeaderView:0x7fb3a0d76df0]-(0)-|   (active,names: '|':_UIDatePickerCalendarContentStackView:0x7fb3a0d76c60 )>","<NSLayoutConstraint:0x600003442940 'UIView-Encapsulated-Layout-Width' UIView:0x7fb3a0e78db0.width == 0   (active)>","<NSLayoutConstraint:0x6000034378e0 'UIView-leftMargin-guide-constraint' H:|-(8)-[UILayoutGuide:0x600002e208c0'UIViewLayoutMarginsGuide'](LTR)   (active,names: '|':_UIDatePickerCalendarHeaderView:0x7fb3a0d76df0 )>","<NSLayoutConstraint:0x600003437980 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x600002e208c0'UIViewLayoutMarginsGuide']-(8)-|(LTR)   (active,names: '|':_UIDatePickerCalendarHeaderView:0x7fb3a0d76df0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000034373e0 UILabel:0x7fb3a0d7a0d0.trailing <= _UIDatePickerLinkedLabel:0x7fb3a0d79f10.trailing   (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.

我尝试在静态单元格中为日期选择器设置约束,但错误仍然存​​在。我也试过在没有任何限制的情况下离开选择器,没有运气。

关于这里可能出现什么问题的任何想法?我可能犯了一些基本错误,但我无法识别。

环境:

  • 斯威夫特 5
  • iOS 14.x

解决方法

查看您的错误消息 UILabel:0x7fb3a0d7a0d0.trailing 我发现您的 Cells 标签约束已添加到 Picker 视图中。

从 Cell 中移除所有选择器约束并使其相对于控制器视图。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。