我在单元格内有自定义 UITableViewCell 和 UIWebView。 UIWebView 有默认的高度限制(20 像素)。
class TableViewCell: UITableViewCell, UIWebViewDelegate {
@IBOutlet weak var webView: UIWebView!
@IBOutlet weak var webViewHeight: NSLayoutConstraint!
func webViewDidFinishLoad(webView: UIWebView) {
webView.frame.size.height = 1
var contentSize = webView.scrollView.contentSize
webView.frame.size.height = contentSize.height
webView.scrollView.frame.size.height = contentSize.height
webViewHeight.constant = contentSize.height
}
}
然后我设置要加载到 webView 中的内容。加载 UIWebViewDelegate 调用 webViewDidFinishLoad
后,我更改了高度约束。此时我在调试区域看到约束错误。
2016-07-06 09:11:20.492 TEST CELL[1746:56476] 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.
(
"<NSLayoutConstraint:0x7fbc087088f0 V:[UIWebView:0x7fbc0871eb70(50)]>",
"<NSLayoutConstraint:0x7fbc086b46f0 UIWebView:0x7fbc0871eb70.top == UITableViewCellContentView:0x7fbc0871dea0.topMargin>",
"<NSLayoutConstraint:0x7fbc086b4740 UITableViewCellContentView:0x7fbc0871dea0.bottomMargin == UIWebView:0x7fbc0871eb70.bottom>",
"<NSLayoutConstraint:0x7fbc08700c10 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fbc0871dea0(59)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fbc087088f0 V:[UIWebView:0x7fbc0871eb70(50)]>
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.
如何在加载后正确设置新的约束常量?
最佳答案
webViewDidFinishLoad
可以调用多次,每次加载完一个帧,所以你必须检查这个属性:
if (webview.isLoading) {
return
} else {
// do my stuff
}
关于ios - 加载后更改 UITableViewCell 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38206617/