How to tell UITableView that a self-sizing cell's height is about to change?

安稳与你 提交于 2019-12-23 01:03:33

问题


I've got a UITableView whose contents are somewhat dynamic. It seems like every time I resize them, I hit this error:

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:0x15dbabf0 WKWebView:0x15da1650.top == UITableViewCellContentView:0x15eaedb0.topMargin>",

    "<NSLayoutConstraint:0x15dbac20 UITableViewCellContentView:0x15eaedb0.bottomMargin == WKWebView:0x15da1650.bottom>",

    "<NSLayoutConstraint:0x15db1360 V:[WKWebView:0x15da1650(158)]>",

    "<NSLayoutConstraint:0x15ed0c00 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x15eaedb0(43.5)]>"

)

It's that last one - the one labeled UIView-Encapsulated-Layout-Height - that is breaking things. I want to get rid of it, or set it to a breaking priority, or something - but I can't seem to be able to tell UITableView to break it. I've tried every possible combination of setNeedsUpdateConstraints and setNeedsLayout on various views. I've tried removing all the constraints from the cell's contentView on updateConstraints. I've tried just calling beginUpdates and endUpdates - in that case, updateConstraints is never called & the constraints are not re-evaluated.

I'm at a loss, here. Without self-sizing cells, just calling begin/endUpdates used to trigger a re-calculation of the cell's dimensions. How do I do it now?


回答1:


The WKWebView which you are using inside the content view is the most possible cause of breaking constraints. Also the height of any webView can be calculated only when the WebView has finished loading. For e.g in UIWebView the height of the UIWebView can be fetched appropriately only in the delgate method "didFinishLoadingWebView" method.

Also, the 'UIView-Encapsulated-Layout-Height' constraints supposedly does not get satisfied as the cell's intrinsic height calculated probably has a different value. Hence one can lower the priority of the relevant cell constraint to allow UIView-Encapsulated-Layout-Height to have precedence.



来源:https://stackoverflow.com/questions/28059581/how-to-tell-uitableview-that-a-self-sizing-cells-height-is-about-to-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!