About WebView content height of access, I believe many people have stepped on the pit, can not get to the exact height, resulting in page layout errors, the search for a lot of information but can not solve the problem, the following is a summary of personal experience: project requirements to achieve H5 article & native Comment effect, the article is loaded H5 link, The comment information is provided by the backend interface and presented as a list. Implementation ideas:
1. WebView load H5 link, set it to TableView Headerview, the comment information below is loaded with cell display.
2. In WebView callback method Webviewdidfinishload, get the height of the Web page content, set to the height of webview, and re-assign WebView to TableView.
Tip: When assigning a view to UITableView's Tableheaderview, you don't need to set the height manually, Headerview automatically uses the view's height.
Like above, a lot of similar methods, whether it is JS acquisition, or Contentsize acquisition, the final results are difficult to get to the exact height, not the method, but:
Webviewdidfinishload Proxy method is called, the page is not necessarily completely complete, there may be a picture has not been loaded out, resulting in the height of the acquisition is small, after a while the picture is loaded, the browser will be re-typesetting the page is big, and we gave a high enough content to show, Causes an exception to be displayed.
The cause of the problem is determined and the solution is considered:
How can I get a change in the height of Web content after webviewdidfinishload?
Answer: Listen!
Specific implementation process:
To webview the ScrollView of the Contentsize property to add listening, whenever the content changes, Contentsize will follow the change, capture this change, in the listening method to implement the code in Webviewdidfinishload, That is, get the latest content highly assigned to WebView:
In addition, using WebView in the cell to obtain a highly inaccurate solution is the same as above, except that the use of webview in the cell involves cell reuse, which causes the sliding list to webview multiple times, which affects performance and recommends cache height.
IOS "Wild path" accurate access to webview content height, adaptive height