IOS "Wild path" accurate access to webview content height, adaptive height

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.