【问题标题】:UITableViewCell Auto-Layout Constraint ProblemsUITableViewCell 自动布局约束问题
【发布时间】:2017-08-03 03:36:17
【问题描述】:

我在调试器中收到Unable to simultaneously satisfy constraints. 错误。我知道这种类型的问题已经被问过很多次了,而且我知道如何跟踪约束以查看问题所在 - 但这个问题对我来说毫无意义。我正在使用UITableViewAutomaticDimension 功能来自动调整单元格的大小。以下是我的限制条件。为了简化图像,图像上的每个对象都有一个指定的高度,除了 postTextView,它具有灵活的高度,以便它可以扩展不同数量的文本。我希望其他所有东西都有一个特定的高度。

一旦约束被打破,它声明它将在控制台中尝试,屏幕上的布局是正确的,一切都按照我的预期运行。

这是我在运行应用程序时遇到的错误:

2017-08-02 23:29:29.932838-0400 StudyHub[35782:3472431] [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:0x60400048e9c0 UIImageView:0x7fe16cd55110.height == 55   (active)>",
"<NSLayoutConstraint:0x60400048b680 UIButton:0x7fe16cd5a1d0'0'.height == 25   (active)>",
"<NSLayoutConstraint:0x60400048ee70 UIStackView:0x7fe16cd5cf60.height == 308   (active)>",
"<NSLayoutConstraint:0x60400048f550 V:|-(8)-[UIImageView:0x7fe16cd55110]   (active, names: '|':UIView:0x7fe16cd543b0 )>",
"<NSLayoutConstraint:0x60400048f640 V:[UIImageView:0x7fe16cd55110]-(8)-[UITextView:0x7fe16d8ff000'This is a test of the pos...']   (active)>",
"<NSLayoutConstraint:0x60400048f780 V:[UITextView:0x7fe16d8ff000'This is a test of the pos...']-(8)-[UIStackView:0x7fe16cd5cf60]   (active)>",
"<NSLayoutConstraint:0x60400048f8c0 V:[UIStackView:0x7fe16cd5cf60]-(8)-[UIButton:0x7fe16cd5a1d0'0']   (active)>",
"<NSLayoutConstraint:0x60400048f910 V:[UIButton:0x7fe16cd5a1d0'0']-(8)-|   (active, names: '|':UIView:0x7fe16cd543b0 )>",
"<NSLayoutConstraint:0x60400048fb90 V:|-(8)-[UIView:0x7fe16cd543b0]   (active, names: '|':UITableViewCellContentView:0x7fe16cd53650 )>",
"<NSLayoutConstraint:0x60400048fc30 V:[UIView:0x7fe16cd543b0]-(8)-|   (active, names: '|':UITableViewCellContentView:0x7fe16cd53650 )>",
"<NSLayoutConstraint:0x600000499460 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fe16cd53650.height == 50   (active)>"
)

    Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x60400048ee70 UIStackView:0x7fe16cd5cf60.height == 308   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2017-08-02 23:29:29.962013-0400 StudyHub[35782:3472431] [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:0x60400048e9c0 UIImageView:0x7fe16cd55110.height == 55   (active)>",
"<NSLayoutConstraint:0x60400048b680 UIButton:0x7fe16cd5a1d0'0'.height == 25   (active)>",
"<NSLayoutConstraint:0x60400048f550 V:|-(8)-[UIImageView:0x7fe16cd55110]   (active, names: '|':UIView:0x7fe16cd543b0 )>",
"<NSLayoutConstraint:0x60400048f640 V:[UIImageView:0x7fe16cd55110]-(8)-[UITextView:0x7fe16d8ff000'This is a test of the pos...']   (active)>",
"<NSLayoutConstraint:0x60400048f780 V:[UITextView:0x7fe16d8ff000'This is a test of the pos...']-(8)-[UIStackView:0x7fe16cd5cf60]   (active)>",
"<NSLayoutConstraint:0x60400048f8c0 V:[UIStackView:0x7fe16cd5cf60]-(8)-[UIButton:0x7fe16cd5a1d0'0']   (active)>",
"<NSLayoutConstraint:0x60400048f910 V:[UIButton:0x7fe16cd5a1d0'0']-(8)-|   (active, names: '|':UIView:0x7fe16cd543b0 )>",
"<NSLayoutConstraint:0x60400048fb90 V:|-(8)-[UIView:0x7fe16cd543b0]   (active, names: '|':UITableViewCellContentView:0x7fe16cd53650 )>",
"<NSLayoutConstraint:0x60400048fc30 V:[UIView:0x7fe16cd543b0]-(8)-|   (active, names: '|':UITableViewCellContentView:0x7fe16cd53650 )>",
"<NSLayoutConstraint:0x6000004990f0 'UISV-canvas-connection' UIStackView:0x7fe16cd5cf60.top == UIStackView:0x7fe16cd5d9c0.top   (active)>",
"<NSLayoutConstraint:0x600000498fb0 'UISV-canvas-connection' V:[UIStackView:0x7fe16cd5e110]-(0)-|   (active, names: '|':UIStackView:0x7fe16cd5cf60 )>",
"<NSLayoutConstraint:0x600000499190 'UISV-fill-equally' UIStackView:0x7fe16cd5e110.height == UIStackView:0x7fe16cd5d9c0.height   (active)>",
"<NSLayoutConstraint:0x6000004991e0 'UISV-spacing' V:[UIStackView:0x7fe16cd5d9c0]-(8)-[UIStackView:0x7fe16cd5e110]   (active)>",
"<NSLayoutConstraint:0x600000499460 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fe16cd53650.height == 50   (active)>"
)

    Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x6000004991e0 'UISV-spacing' V:[UIStackView:0x7fe16cd5d9c0]-(8)-[UIStackView:0x7fe16cd5e110]   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2017-08-02 23:29:29.963167-0400 StudyHub[35782:3472431] [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:0x60400048e9c0 UIImageView:0x7fe16cd55110.height == 55   (active)>",
"<NSLayoutConstraint:0x60400048b680 UIButton:0x7fe16cd5a1d0'0'.height == 25   (active)>",
"<NSLayoutConstraint:0x60400048f550 V:|-(8)-[UIImageView:0x7fe16cd55110]   (active, names: '|':UIView:0x7fe16cd543b0 )>",
"<NSLayoutConstraint:0x60400048f640 V:[UIImageView:0x7fe16cd55110]-(8)-[UITextView:0x7fe16d8ff000'This is a test of the pos...']   (active)>",
"<NSLayoutConstraint:0x60400048f780 V:[UITextView:0x7fe16d8ff000'This is a test of the pos...']-(8)-[UIStackView:0x7fe16cd5cf60]   (active)>",
"<NSLayoutConstraint:0x60400048f8c0 V:[UIStackView:0x7fe16cd5cf60]-(8)-[UIButton:0x7fe16cd5a1d0'0']   (active)>",
"<NSLayoutConstraint:0x60400048f910 V:[UIButton:0x7fe16cd5a1d0'0']-(8)-|   (active, names: '|':UIView:0x7fe16cd543b0 )>",
"<NSLayoutConstraint:0x60400048fb90 V:|-(8)-[UIView:0x7fe16cd543b0]   (active, names: '|':UITableViewCellContentView:0x7fe16cd53650 )>",
"<NSLayoutConstraint:0x60400048fc30 V:[UIView:0x7fe16cd543b0]-(8)-|   (active, names: '|':UITableViewCellContentView:0x7fe16cd53650 )>",
"<NSLayoutConstraint:0x600000499460 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fe16cd53650.height == 50   (active)>"
)

    Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x60400048e9c0 UIImageView:0x7fe16cd55110.height == 55   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2017-08-02 23:29:29.963961-0400 StudyHub[35782:3472431] [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:0x60400048b680 UIButton:0x7fe16cd5a1d0'0'.height == 25   (active)>",
"<NSLayoutConstraint:0x60400048f550 V:|-(8)-[UIImageView:0x7fe16cd55110]   (active, names: '|':UIView:0x7fe16cd543b0 )>",
"<NSLayoutConstraint:0x60400048f640 V:[UIImageView:0x7fe16cd55110]-(8)-[UITextView:0x7fe16d8ff000'This is a test of the pos...']   (active)>",
"<NSLayoutConstraint:0x60400048f780 V:[UITextView:0x7fe16d8ff000'This is a test of the pos...']-(8)-[UIStackView:0x7fe16cd5cf60]   (active)>",
"<NSLayoutConstraint:0x60400048f8c0 V:[UIStackView:0x7fe16cd5cf60]-(8)-[UIButton:0x7fe16cd5a1d0'0']   (active)>",
"<NSLayoutConstraint:0x60400048f910 V:[UIButton:0x7fe16cd5a1d0'0']-(8)-|   (active, names: '|':UIView:0x7fe16cd543b0 )>",
"<NSLayoutConstraint:0x60400048fb90 V:|-(8)-[UIView:0x7fe16cd543b0]   (active, names: '|':UITableViewCellContentView:0x7fe16cd53650 )>",
"<NSLayoutConstraint:0x60400048fc30 V:[UIView:0x7fe16cd543b0]-(8)-|   (active, names: '|':UITableViewCellContentView:0x7fe16cd53650 )>",
"<NSLayoutConstraint:0x600000499460 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fe16cd53650.height == 50   (active)>"
)

    Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x60400048b680 UIButton:0x7fe16cd5a1d0'0'.height == 25   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2017-08-02 23:29:29.964742-0400 StudyHub[35782:3472431] [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:0x60400048f550 V:|-(8)-[UIImageView:0x7fe16cd55110]   (active, names: '|':UIView:0x7fe16cd543b0 )>",
"<NSLayoutConstraint:0x60400048f640 V:[UIImageView:0x7fe16cd55110]-(8)-[UITextView:0x7fe16d8ff000'This is a test of the pos...']   (active)>",
"<NSLayoutConstraint:0x60400048f780 V:[UITextView:0x7fe16d8ff000'This is a test of the pos...']-(8)-[UIStackView:0x7fe16cd5cf60]   (active)>",
"<NSLayoutConstraint:0x60400048f8c0 V:[UIStackView:0x7fe16cd5cf60]-(8)-[UIButton:0x7fe16cd5a1d0'0']   (active)>",
"<NSLayoutConstraint:0x60400048f910 V:[UIButton:0x7fe16cd5a1d0'0']-(8)-|   (active, names: '|':UIView:0x7fe16cd543b0 )>",
"<NSLayoutConstraint:0x60400048fb90 V:|-(8)-[UIView:0x7fe16cd543b0]   (active, names: '|':UITableViewCellContentView:0x7fe16cd53650 )>",
"<NSLayoutConstraint:0x60400048fc30 V:[UIView:0x7fe16cd543b0]-(8)-|   (active, names: '|':UITableViewCellContentView:0x7fe16cd53650 )>",
"<NSLayoutConstraint:0x600000499460 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fe16cd53650.height == 50   (active)>"
)

    Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x60400048f640 V:[UIImageView:0x7fe16cd55110]-(8)-[UITextView:0x7fe16d8ff000'This is a test of the pos...']   (active)>

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

【问题讨论】:

  • 这很复杂,但这里有一个猜测。如果堆栈视图的高度不受限制,它可以改变大小,但中心必须距离底部 38。这意味着到底部的距离会有所不同。然而,到底部的距离也受到下方按钮的间距以及它们与底部的间距的限制。这些限制似乎是固定的。也许尝试删除该中心约束,看看会发生什么。
  • 我同意。要使自动行大小调整起作用,您需要从上到下的连续垂直约束链;由于按钮的底部约束和堆栈视图的底部约束,您有两个。这导致了冲突
  • 我看到我提供的图形存在一个小问题并对其进行了更新。垂直约束仅在底部中心按钮上,而不在堆栈视图上。我从上到下有连续的垂直约束,除了中心文本视图没有设置垂直高度,因此它是有弹性的。

标签: ios swift xcode uitableview autolayout


【解决方案1】:

似乎您还需要在 imageView 上设置宽度约束。您还可以通过单击下面的调试视图层次结构尝试调试您的自动布局约束。

【讨论】:

    【解决方案2】:

    在 Swift 4 中我正在创建一个演示项目:- 根据您的 UIViewController。

    UIViewController 演示:

    请检查我希望它能起作用的约束:-

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-08
      • 2015-04-06
      • 2018-07-06
      • 2016-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-16
      相关资源
      最近更新 更多