我的一个 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/