【发布时间】:2014-09-23 10:19:27
【问题描述】:
我有一个在 iOS 8 下运行的 UITableView,我正在使用情节提要中约束的自动单元格高度。
我的一个单元格包含一个 UITextView,我需要它根据用户输入来收缩和扩展 - 点击以收缩/扩展文本。
我通过向文本视图添加运行时约束并更改约束上的常量以响应用户事件来做到这一点:
-(void)collapse:(BOOL)collapse; {
_collapsed = collapse;
if(collapse)
[_collapsedtextHeightConstraint setConstant: kCollapsedHeight]; // 70.0
else
[_collapsedtextHeightConstraint setConstant: [self idealCellHeightToShowFullText]];
[self setNeedsUpdateConstraints];
}
每当我这样做时,我都会将其包装在 tableView 更新中并调用 [tableView setNeedsUpdateConstraints]:
[tableView beginUpdates];
[_briefCell collapse:!_showFullBriefText];
[tableView setNeedsUpdateConstraints];
// I have also tried
// [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
// with exactly the same results.
[tableView endUpdates];
当我这样做时,我的单元格确实会扩展(并在此过程中进行动画处理),但我会收到约束警告:
2014-07-31 13:29:51.792 OneFlatEarth[5505:730175] 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:0x7f94dced2b60 V:[UITextView:0x7f94d9b2b200'Brief text: Lorem Ipsum i...'(388)]>",
"<NSLayoutConstraint:0x7f94dced2260 V:[UITextView:0x7f94d9b2b200'Brief text: Lorem Ipsum i...']-(15)-| (Names: '|':UITableViewCellContentView:0x7f94de5773a0 )>",
"<NSLayoutConstraint:0x7f94dced2350 V:|-(6)-[UITextView:0x7f94d9b2b200'Brief text: Lorem Ipsum i...'] (Names: '|':UITableViewCellContentView:0x7f94de5773a0 )>",
"<NSLayoutConstraint:0x7f94dced6480 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7f94de5773a0(91)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7f94dced2b60 V:[UITextView:0x7f94d9b2b200'Brief text: Lorem Ipsum i...'(388)]>
388 是我计算的高度,UITextView 上的其他限制是我从 Xcode/IB 获得的。
最后一个困扰着我——我猜UIView-Encapsulated-Layout-Height 是单元格首次渲染时的计算高度——(我将UITextView 高度设置为 >= 70.0)但事实并非如此这个派生的约束然后推翻更新的用户 cnstraint 似乎是正确的。
更糟糕的是,虽然布局代码说它试图打破我的高度限制,但它没有 - 它继续重新计算单元格高度,并且一切都按照我的意愿绘制。
那么,NSLayoutConstraintUIView-Encapsulated-Layout-Height 是什么(我猜它是自动调整单元格大小的计算高度),我应该如何强制它干净地重新计算?
【问题讨论】:
-
交叉发布到 Apple 开发者论坛:devforums.apple.com/thread/238803
-
我通过以下方式解决了类似的问题,它适用于 iOS 7/8。 1) 将其中一个约束优先级降低到 750。我会尝试第一个或第二个 2) 在 awakeFromNib 集
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight;的单元子类中。我认为最初设置自动调整掩码会阻止添加最后一个约束。我在这里找到了这个解决方案:github.com/wordpress-mobile/WordPress-iOS/commit/… -
@RogerNolan,有什么消息吗?我在 Interface Builder 中使用 Auto-layout 时发现了同样的问题。有些单元格会导致此问题,有些则不会。
-
我不认为添加自动调整大小标记是一个好的解决方案。
-
@RogerNolan 您是否在执行这些布局更改之前在 IB 中生成此单元格?我一直在调试同样的问题,但我没有添加任何额外的约束。我设法通过从头开始重建视图来抑制警告,当我区分 2 个故事板文件时,唯一的区别是带有警告的版本在其定义中缺少
<rect key="frame" x="0.0" y="0.0" width="600" height="110"/>行,这让我相信这是一个 IB 错误。至少我的还是这样。
标签: ios uitableview cocoa-touch autolayout ios-autolayout