gpt4 book ai didi

ios - 具有自动布局的 UITableViewCell 中的动态大小的 UIWebView - 违反约束

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:21:59 33 4
gpt4 key购买 nike

我有一个 UITableViewCell其中包含 UIWebView .表格 View 单元格根据 WebView 内容调整其高度。我一切正常,但是当加载 View 时,我在调试器中遇到约束违反异常(应用程序继续运行并且功能正常,但我想尽可能解决此异常)。

我是如何设置它的:

TableView 像这样设置单元格高度:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section == 0) {
[_topCell layoutIfNeeded];
CGFloat finalHeight = [_topCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return finalHeight + 1;
}

单元格约束如下:

  1. 从单元格的 contentView(顶部)到 webView 的任意 7px 偏移量
  2. Web View 具有 62px 的任意固定高度限制(内容加载后将展开)
  3. 从 webView 到单元格的 contentView(底部)的任意 8px 偏移

在我的 viewDidLoad 中,我告诉 webView 去加载一个 URL,然后在 webViewDidFinishLoad 中,我像这样更新 WebView 高度约束

-(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 View 高度的约束将被打破,但是 web View 确实正确设置了它的高度,并且 tableview 呈现得非常好。

根据我的猜测,新增加的 WebView 高度限制(在 WebView 加载后约为 500px)似乎会与 <NSAutoresizingMaskLayoutConstraint:0x10967c210 h=--& v=--& V:[cellContent(78)] 冲突。将单元格高度设置为 78(由界面生成器放置在那里)。这是有道理的,但是我不希望该单元格内容具有 78 像素的固定高度,我希望它增加它的高度,并且在功能上,它实际上是这样做的,只是有这些异常(exception)。

  • 我试过设置 _topCell.contentView.translatesAutoresizingMaskIntoConstraints = NO;尝试删除 NSAutoresizingMaskLayoutConstraint - 这会停止异常,但随后所有其他布局都搞砸了,Web View 在表格 View 中间无缘无故地高了大约 10 像素。

  • 我也试过设置 _topCell.contentView.autoresizingMask |= UIViewAutoresizingFlexibleHeight;viewDidLoad希望影响 contentView 78px 高度限制,但这没有效果

任何帮助将不胜感激

最佳答案

另一种答案,最终要简单得多:

将 webViewHeight 约束的优先级设置为非必需的值。效果很好,没有警告。我建议使用这个 :-)

关于ios - 具有自动布局的 UITableViewCell 中的动态大小的 UIWebView - 违反约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23901082/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com