【问题标题】:UITableViewAutomaticDimension not working properly in iOS 9UITableViewAutomaticDimension 在 iOS 9 中无法正常工作
【发布时间】:2015-12-17 23:53:02
【问题描述】:

在我的应用中,我有一个固定比例 (16x9) 的自定义 tableViewCells。为了实现这一点,我在单元格中放置了一个视图,将其固定到其父视图(尽管我在界面生成器中完成了它:V/H:|-[innerView]-|)。 另外,我对其进行了比率约束。

在我的 tableViewController 中,我使用 UITableViewAutomaticDimension 作为表格单元格高度。

估计的行高为 180,这是单元格在 320 像素宽的显示器上的确切大小(正如我所见,我就是这样做的)。

我仍在为 8.4 进行部署,但在装有 iOS 9 的设备上运行项目时,我收到大量自动布局警告,尽管一切正常且看起来很完美。

警告本身是绝对正确的。有两个约束是我不想要的——iOS 自己添加的。

2015-09-29 11:24:57.771 app[1039:324736] 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:0x147d3e0d0 UIView:0x14901ff70.height == 0.5625*UIView:0x14901ff70.width>",
    "<NSLayoutConstraint:0x147dd0210 H:|-(0)-[UIView:0x14901ff70]   (Names: '|':UITableViewCellContentView:0x14901f960 )>",
    "<NSLayoutConstraint:0x147deeca0 V:|-(0)-[UIView:0x14901ff70]   (Names: '|':UITableViewCellContentView:0x14901f960 )>",
    "<NSLayoutConstraint:0x149053c30 V:[UIView:0x14901ff70]-(0)-|   (Names: '|':UITableViewCellContentView:0x14901f960 )>",
    "<NSLayoutConstraint:0x147dbc2b0 H:[UIView:0x14901ff70]-(0)-|   (Names: '|':UITableViewCellContentView:0x14901f960 )>",
    "<NSLayoutConstraint:0x149070800 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x14901f960(179.5)]>",
    "<NSLayoutConstraint:0x1490707b0 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x14901f960(320)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x147d3e0d0 UIView:0x14901ff70.height == 0.5625*UIView:0x14901ff70.width>

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.

我在这里看到的唯一东西是 iOS 神奇地减去了 0.5 个像素。

【问题讨论】:

  • 我在这里看到的唯一的东西是 iOS 以某种方式神奇地减去了丢失的 0.5 个像素?这个信息在哪里?
  • @somexyz 读取记录的约束:V:[UITableViewCellContentView:0x14da0ff60(179.5)]

标签: ios uitableview ios9


【解决方案1】:

您似乎在两个不同的视图上添加了两个比率约束。

应该有的,

<NSLayoutConstraint:0x14d939e00 UIView:0x14da107f0.width ==
1.77778*UIView:0x14da107f0.height>

在 UIView 上添加了内存地址 0x14d939e00,地址为 0x14da107f0。

另一个坏了。

<NSLayoutConstraint:0x14c5eae90 UIView:0x14c5ead30.height ==
    0.5625*UIView:0x14c5ead30.width>

这是在 UIView (0x14c5ead30) 上添加的。查找此视图并删除此比率约束。

【讨论】:

  • 对不起,我把一个日志的开头和另一个日志的结尾弄混了。这就是为什么地址和值都不适合在一起的原因。只有一个约束,因为它在 iOS 8 中运行良好,我认为这不是一般的自动布局问题。
【解决方案2】:

问题在于 tableview 自动添加的两个约束。 UIView-Encapsulated-Layout-Height 和 width 大概是表格视图在初始加载时根据cell当时的约束为cell计算的高度和宽度。

"<NSLayoutConstraint:0x149070800 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x14901f960(179.5)]>",
"<NSLayoutConstraint:0x1490707b0 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x14901f960(320)]>"

由于这些约束的优先级是 1000,您可以做的是降低您的高度和宽度约束优先级,并在需要时(在加载时)应用封装的大小。

【讨论】:

  • 这确实有帮助。感谢您对时间(已加载)的简短解释。你能说说 iOS 8 和 iOS 9 之间的区别吗?我绝对需要/想知道为什么只有 iOS 9 的做法有所不同...
  • 我真的不知道 iOS 9 中的自动布局引擎发生了什么变化,但这也发生在 iOS 8 中。
  • 是的。这可能也发生在 iOS 8 中,就您所说的这种或多或少通用的自动布局警告而言。在我的情况下,这个警告只出现在 iOS9,而不是 8,因此我打开了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-20
  • 1970-01-01
  • 2017-01-06
  • 1970-01-01
  • 2020-11-04
相关资源
最近更新 更多