Tips on uiwebview

Source: Internet
Author: User

1. Use flashscrollindicators in uiwebview

When using uiscrollview, we can use the flashscrollindicators method to display the scroll icon and then disappear, informing users that the page can be rolled and more content will be available later. Uiwebview depends on uiscrollview, but it does not have the flashscrollindicators method. However, you can use this method in other ways, as shown below.

for (id subView in [webView subviews]) {  if ([subView respondsToSelector:@selector(flashScrollIndicators)]) {    [subView flashScrollIndicators];  }}


The preceding code snippets can be used in the webviewdidfinishload callback. After the webpage content is loaded, the flash displays the scroll ID.

2. Remove background shadows of uiwebview

Assume that a uiwebview is black and white. When you drag a uiwebview, the background shadow is displayed, as shown in.

You can use the following code to remove the shadow and set the background to white.

if ([[webView subviews] count] > 0) {  // hide the shadows  for (UIView* shadowView in [[[webView subviews] objectAtIndex:0] subviews]) {    [shadowView setHidden:YES];  }  // show the content  [[[[[webView subviews] objectAtIndex:0] subviews] lastObject] setHidden:NO];}webView.backgroundColor = [UIColor whiteColor];


The drag effect after using this code.

3. Obtain the height of uiwebview Based on the Content

Sometimes you need to adjust the height of the uiwebview based on different content, so that the uiwebview can be loaded with all the content without dragging and the content will not be left blank. You can obtain the appropriate height of the uiwebview Based on the loaded content in either of the two ways. However, the height of the uiwebview must be obtained only after the webpage content is loaded. That is, it must be used in the webviewdidfinishload callback.

1. Use the sizethatfits method.

- (void)webViewDidFinishLoad:(UIWebView *)webView {    CGRect frame = webView.frame;    frame.size.height = 1;    webView.frame = frame;    CGSize fittingSize = [webView sizeThatFits:CGSizeZero];    frame.size = fittingSize;    webView.frame = frame;}


There is a problem with the sizethatfits method. If the current uiview size is larger than the right size, the current size is returned and the most suitable size value is not returned. Therefore, before using sizethatfits, set the height of uiwebview to the minimum value, that is, 1, and then use sizethatfits to return a proper size.

2. Use Javascript

  - (void)webViewDidFinishLoad:(UIWebView *)webView{    CGRect frame = webView.frame;    NSString *fitHeight = [webview stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"];    frame.size.height = [fitHeight floatValue];    webView.frame = frame;}


Use the stringbyevaluatingjavascriptfromstring method of uiwebview to execute the Javascript Statement document. Body. scrollheight. You can obtain the full text height of the current webpage. Document. Body. offsetheight was used incorrectly during the test.

3. Restrictions

Both methods can only be used when the webpage is loaded. When the uitableviewcell in uitableview contains uiwebview, the most appropriate height of uiwebview cannot be obtained in tableview: heightforrowatindexpath, the appropriate height of uitableviewcell cannot be determined.
Any ideas?

Refer:
Three useful uiwebview tweaks
How to determine uiwebview height based on content, within a variable height
Uitableview?
How to determine the content size of a uiwebview?
Difference between clientheight, offsetheight and scrollheight

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.