ios - 仅在切换 View Controller 时出现 NSLayoutConstraint 错误

标签 ios objective-c uitableview autolayout nslayoutconstraint

我的一个 View Controller 出现一些非常奇怪的行为。

我正在从不同选项卡中的两个不同 UITableViews 导航到同一个 Controller (具有不同的实例)。

当它通过点击 UITableViewCell 显示时,一切都很好,没有错误显示。如果我打开 Controller 并通过选择另一个选项卡离开它,然后通过按原始选项卡导航回它,我会收到大量的 NSLayoutConstraint 错误。

实际上,我的单​​元格中没有任何约束, Storyboard 中单元格的内容 View 中也没有任何 UI 元素,因为我是以编程方式添加所有这些元素的。我也没有在代码中定义任何约束。

完整的错误是here ,因为它太大而无法合理地粘贴到 SO 中。

节选如下:

2015-04-11 16:49:23.889 TradingPost[6982:60b] 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:0x155e5250 H:[UILabel:0x155d8150(158)]>",
"<NSLayoutConstraint:0x155ed9e0 H:|-(8)-[UILabel:0x155d8150]   (Names: '|':UITableViewCellContentView:0x155d4a80 )>",
"<NSLayoutConstraint:0x155eda30 H:[UILabel:0x155d8150]-(47)-[UIView:0x155e01a0]>",
"<NSLayoutConstraint:0x155f1bc0 UIView:0x155e01a0.trailing == UITableViewCellContentView:0x155d4a80.trailing - 8>",
"<NSAutoresizingMaskLayoutConstraint:0x156b7640 h=--& v=--& H:[UITableViewCellContentView:0x155d4a80(0)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x155e5250 H:[UILabel:0x155d8150(158)]>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

补充:经过进一步调查,我发现我仍然遇到错误,即使我从 Controller 中删除了所有 UI 生成代码并从 Storyboard 中删除了所有约束。在这一点上,我怀疑是巫术。

最佳答案

即使您没有显式创建任何约束,自动布局系统也可以从与每个 View 关联的 autoresizingMask 掩码创建它们。您可以尝试将 translatesAutoresizingMaskIntoConstraints 设置为 NO。但是,如果您使用自动布局和尺寸类来创建适应各种屏幕尺寸的界面,您可能需要接受约束。在这种情况下,您最好在代码中明确设置相关约束。

关于ios - 仅在切换 View Controller 时出现 NSLayoutConstraint 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29579881/

相关文章:

ios - 在 View Controller 中绑定(bind)元组或字符串以使用 RXSwift 查看模型

ios - 什么是 kCFErrorDomainCFNetwork Code=303

ios - 在 Storyboard中使用自定义 tableviewcell 时,IBOutlet 为零

ios - UITableViewController VS UICollectionView

ios - 滚动表格 View 时单元格变为空白(快速)

uitableview - 在自定义 UlTableViewCell 中使用 UITextField 时 resignFirstResponder 崩溃

ios - 文件访问读取以前写入 NSDocumentDirectory 的图像失败

ios - Swift:如何在 tableView 中滚动时停止滚动,然后在最后一个 tableView 项目之后继续滚动?

iphone - ios编程中切换UIViews的正确方法

ios - Restkit 0.20 JSON 映射以及额外的离线数据