动态 UITableViewCell 的子视图中的 AutoLayout 问题
AutoLayout issue in dynamic UITableViewCell's subviews
虽然我似乎已经掌握了在故事板的 UIView 上设置视图约束的要点,但我似乎无法弄清楚为什么约束在动态单元格原型 ContentView 的子视图中无法正常工作。
单元格非常简单:一个 UIImageView 背景填充整个单元格,在它前面是另一个 UIImage 和一个标签,如下所示:
后台UIImageView有以下4个约束:
0 固定为:trail、bottom、top 和 leading space 到 superview
个人资料图片具有固定的宽度和高度,并且相对于主视图具有恒定的 13pts space 并且在容器中具有垂直中心
标签有一个 8pt 前导 space 到个人资料图片,尾随 space 到超级视图,并且在容器中有一个垂直中心。
这就是我得到的:
我错过了什么吗?我研究了这个问题,但没有答案解决了这个问题。但是,我尝试在加载单元格后在 cellForRowAtIndexPath 中添加以下行:
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()
在运行时出现以下错误:
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)
(
"NSLayoutConstraint:0x7fe5434852c0 H:[UIImageView:0x7fe543484450(75)]",
"NSLayoutConstraint:0x7fe5436c5cd0 H:|-(12)-[UIImageView:0x7fe543484450] (Names: '|':UITableViewCellContentView:0x7fe5434820f0 )",
"NSLayoutConstraint:0x7fe5436c34e0 H:[UILabel:0x7fe543487430'Joseph']-(8)-| (Names: '|':UITableViewCellContentView:0x7fe5434820f0 )",
"NSLayoutConstraint:0x7fe5436bcca0 H:[UIImageView:0x7fe543484450]-(8)-[UILabel:0x7fe543487430'Joseph']",
"NSLayoutConstraint:0x7fe5436cb110 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x7fe5434820f0(0)]"
)
Will attempt to recover by breaking constraint
NSLayoutConstraint:0x7fe5434852c0 H:[UIImageView:0x7fe543484450(75)]
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.
有什么线索吗?
嗯,问题是优先级。
你有背景图片,它的尺寸根据你放入其中的图片而定,还有一张个人资料图片,高度固定,顶部和底部恒定 space 13pts。
Table 可能有分隔符,并且添加了额外的 space.
所以建议:降低例如底部 13pts 限制从 1000 到 750。这应该删除警告。
虽然我似乎已经掌握了在故事板的 UIView 上设置视图约束的要点,但我似乎无法弄清楚为什么约束在动态单元格原型 ContentView 的子视图中无法正常工作。
单元格非常简单:一个 UIImageView 背景填充整个单元格,在它前面是另一个 UIImage 和一个标签,如下所示:
后台UIImageView有以下4个约束: 0 固定为:trail、bottom、top 和 leading space 到 superview
个人资料图片具有固定的宽度和高度,并且相对于主视图具有恒定的 13pts space 并且在容器中具有垂直中心
标签有一个 8pt 前导 space 到个人资料图片,尾随 space 到超级视图,并且在容器中有一个垂直中心。
这就是我得到的:
我错过了什么吗?我研究了这个问题,但没有答案解决了这个问题。但是,我尝试在加载单元格后在 cellForRowAtIndexPath 中添加以下行:
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()
在运行时出现以下错误:
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)
(
"NSLayoutConstraint:0x7fe5434852c0 H:[UIImageView:0x7fe543484450(75)]", "NSLayoutConstraint:0x7fe5436c5cd0 H:|-(12)-[UIImageView:0x7fe543484450] (Names: '|':UITableViewCellContentView:0x7fe5434820f0 )", "NSLayoutConstraint:0x7fe5436c34e0 H:[UILabel:0x7fe543487430'Joseph']-(8)-| (Names: '|':UITableViewCellContentView:0x7fe5434820f0 )", "NSLayoutConstraint:0x7fe5436bcca0 H:[UIImageView:0x7fe543484450]-(8)-[UILabel:0x7fe543487430'Joseph']", "NSLayoutConstraint:0x7fe5436cb110 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x7fe5434820f0(0)]" )Will attempt to recover by breaking constraint NSLayoutConstraint:0x7fe5434852c0 H:[UIImageView:0x7fe543484450(75)]
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.
有什么线索吗?
嗯,问题是优先级。
你有背景图片,它的尺寸根据你放入其中的图片而定,还有一张个人资料图片,高度固定,顶部和底部恒定 space 13pts。
Table 可能有分隔符,并且添加了额外的 space.
所以建议:降低例如底部 13pts 限制从 1000 到 750。这应该删除警告。