0
votes

Thank you for looking at my question. Currently, I have several input fields and a WKWebView embedded in a UIScrollView. Before any events fire, all the subviews fit inside the scroll view with no issue. I'm dynamically setting the WK's height based on document.body.scrollHeight which is captured in the DidFinishNavigation delegate located in WKNavigationDelegate. After the WK's height is set, the WK extends past the view-able content. Here's the code which I'm trying to force the scrollview to resize itself.

[Export("webView:didFinishNavigation:")] public async void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
{

        //get the webView's initial height
        var initialHeight = webView.Frame.Height;

        //get height of HTML's document.body.scrollHeight
        var contentHeight = await GetContentHeight();

        //create new frame for webview
        CGRect newWebViewFrame = new CGRect(webView.Frame.X, webView.Frame.Y, webView.Frame.Width, contentHeight);

        //set webview's frame
        webView.Frame = newWebViewFrame;

        //get the difference of webView's initial height and webView's current height
        var differenceInHeight = contentHeight - initialHeight;

        //create new cgrect and set the height to svMain's height + the difference in height of the HTML document
        CGRect newScrollViewFrame = new CGRect(0, 0, svMainScroller.Frame.Width, svMainScroller.Frame.Height + differenceInHeight);

        //set MainScroller's frame
        svMainScroller.Frame = newScrollViewFrame;

        //force scrolling
        svMainScroller.ScrollEnabled = true;

        //scrolling should be handled in the main scroller
        webView.ScrollView.ScrollEnabled = false;

        svMainScroller.SizeToFit();
    }

The desired effect is to have the scroll view be able to scroll to the end of the newly defined height. Any tips on how would I go about doing that would be greatly appreciated.

1

1 Answers

0
votes

Updating the frame of the scroll view was the problem. My guess is that if the frame is big enough to contain all of the contents, then there's no need to scroll. So, I updated the scroll view's ContentSize instead of updating its frame.

svMainScroller.ContentSize = new CGSize(View.Frame.Width, View.Frame.Height + differenceInHeight);

Also, as a side note, verify that wkwebview is added as a subview to the scroll view and not the main view.