我有一个UITableViewCell
包含的UIWebView
。表格视图单元格根据Web视图的内容调整其高度。我一切都很好,但是当视图加载时,我在调试器中遇到了一个违反约束的异常(该应用程序继续运行并且在功能上正常运行,但是我想尽可能解决这个异常)。
TableView设置像这样的单元格高度:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section == 0) {
[_topCell layoutIfNeeded];
CGFloat finalHeight = [_topCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return finalHeight + 1;
}
单元格约束如下:
在我的viewDidLoad中,我告诉webView去加载一个URL,在中webViewDidFinishLoad
,我更新Web视图的高度约束,像这样
-(void)webViewDidFinishLoad:(UIWebView *)webView {
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
// fittingSize is approx 500
[self.tableView beginUpdates];
// Exceptions happen on the following line setting the constant
_topCell.webViewHeightConstraint.constant = fittingSize.height;
[_topCell layoutSubviews];
[self.tableView endUpdates];
}
异常如下所示:
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:0x10964b250 V:[webView(62)] (Names: webView:0x109664a00 )>",
"<NSLayoutConstraint:0x109243d30 V:|-(7)-[webView] (Names: webView:0x109664a00, cellContent:0x1092436f0, '|':cellContent:0x1092436f0 )>",
"<NSLayoutConstraint:0x109243f80 V:[webView]-(8)-| (Names: cellContent:0x1092436f0, webView:0x109664a00, '|':cellContent:0x1092436f0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x10967c210 h=--& v=--& V:[cellContent(78)] (Names: cellContent:0x1092436f0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x10964b250 V:[webView(62)] (Names: webView:0x109664a00 )>
这似乎有点不可思议。这意味着设置Web视图高度的约束将被打破,但是Web视图的确可以正确设置其高度,并且tableview呈现得很好。
根据我的猜测,似乎新增加的Web视图高度限制(在Web视图加载后大约为500px)将与<NSAutoresizingMaskLayoutConstraint:0x10967c210 h=--& v=--& V:[cellContent(78)]
将单元格高度设置为78(由界面构建器放置在那里)相冲突。这是有道理的,但是我不希望该单元格的内容具有78px的固定高度,我希望它增加其高度,并且在功能上,除这些例外情况外,它实际上是这样做的。
我尝试设置_topCell.contentView.translatesAutoresizingMaskIntoConstraints = NO;
以尝试删除NSAutoresizingMaskLayoutConstraint
-这将停止例外,但是所有其他布局都被弄乱了,并且Web视图在表格视图中间居高约10像素,这是毫无原因的。
我也试着设置_topCell.contentView.autoresizingMask |= UIViewAutoresizingFlexibleHeight;
在viewDidLoad
有希望影响内容查看78px高度约束,但这并没有影响
任何帮助将非常感激
一个替代答案,最终变得简单得多:
将webViewHeight约束的优先级设置为非必需的优先级。效果很好,没有警告。我建议去这个:-)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句