WKWebView + contentInset does wrong content size

In iOS, when you set the contentInset in the scroll of the WKWebView , it seems the web view thinks the page content is bigger than it. For example, if you give a mostly blank page top or bottom contentInset , you can scroll down the page even if there is nothing to scroll there.

Is this the expected behavior? Is there a workaround that allows me to use contentInset ?

+7
ios webkit wkwebview
source share
2 answers

The problem is that WKWebView always seems to use its frame as its viewport size, neglecting the subtraction of scrollView contentInset . I managed to find a stephan-leroux workaround .

The idea is to set the WKWebView frame to the desired viewport size, either manually or using auto-detection restrictions. To restore the scroll down effect of the contentInset , disable scrollView masksToBounds :

 wkWebview.scrollView.layer.masksToBounds = NO; 
+4
source share

The weiyin sentence (resizing the web search frame) worked for me.

Just wanted to share a code example:

 CGFloat topInset = ...; web.scrollView.layer.masksToBounds = NO; [web.scrollView setContentInset:UIEdgeInsetsMake(topInset, 0, 0, 0)]; [web setFrame:CGRectMake(0, 0, web.width, web.height-topInset)]; 
+1
source share

All Articles