Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding header view to WKWebView ScrollView

Has anyone managed to successfully add a header or footer view to a WKWebView ScrollView?

I'm currently trying to do this using the method described here for a UIWebView Adding a header view to a UIWebView similar to Safari and Articles.

When this method is used in a WKWebView the content view origin.y is correctly changed but content is cut off at the bottom.

Using the scroll view content offset is also not possible as it breaks fixed positioned CSS elements in the web view.

like image 952
Keith Peck Avatar asked Aug 12 '15 09:08

Keith Peck


1 Answers

In webView Delegate method

- (void)webViewDidFinishLoad:(UIWebView *)webView

add the following codebase,

mainWebViewObj.scrollView.contentInset = UIEdgeInsetsMake(headerView.frame.size.height,0.0,headerView.frame.size.height,0.0);
mainWebViewObj.scrollView.backgroundColor = [UIColor whiteColor];

if(![headerView superview])
{
    [webView.scrollView addSubview:headerView];
    [webView.scrollView bringSubviewToFront:headerView];
}
[mainWebViewObj.scrollView setContentOffset:
 CGPointMake(0, -mainWebViewObj.scrollView.contentInset.top) animated:NO];

this worked perfect for me. Hope it solves your problem.

like image 133
Surbhi Garg Avatar answered Nov 02 '22 11:11

Surbhi Garg