UIScrollView 在呈现 WKWebView 内容后不滚动
UIScrollView's does not scroll AFTER rendering WKWebView content
感谢您查看我的问题。目前,我有几个输入字段和一个嵌入 UIScrollView 的 WKWebView。在任何事件触发之前,所有子视图都毫无问题地放入滚动视图中。我正在根据 document.body.scrollHeight 动态设置 WK 的高度,这是在位于 WKNavigationDelegate 的 DidFinishNavigation 委托中捕获的。设置 WK 的高度后,WK 会延伸超过可查看的内容。这是我试图强制滚动视图调整自身大小的代码。
[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();
}
期望的效果是让滚动视图能够滚动到新定义高度的末尾。任何关于我将如何去做的提示都将不胜感激。
更新滚动视图的框架是问题所在。我的猜测是,如果框架足够大以包含所有内容,则无需滚动。所以,我更新了滚动视图的 ContentSize 而不是更新它的框架。
svMainScroller.ContentSize = new CGSize(View.Frame.Width, View.Frame.Height + differenceInHeight);
此外,作为旁注,请验证 wkwebview 是否作为子视图添加到滚动视图而不是主视图。
感谢您查看我的问题。目前,我有几个输入字段和一个嵌入 UIScrollView 的 WKWebView。在任何事件触发之前,所有子视图都毫无问题地放入滚动视图中。我正在根据 document.body.scrollHeight 动态设置 WK 的高度,这是在位于 WKNavigationDelegate 的 DidFinishNavigation 委托中捕获的。设置 WK 的高度后,WK 会延伸超过可查看的内容。这是我试图强制滚动视图调整自身大小的代码。
[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();
}
期望的效果是让滚动视图能够滚动到新定义高度的末尾。任何关于我将如何去做的提示都将不胜感激。
更新滚动视图的框架是问题所在。我的猜测是,如果框架足够大以包含所有内容,则无需滚动。所以,我更新了滚动视图的 ContentSize 而不是更新它的框架。
svMainScroller.ContentSize = new CGSize(View.Frame.Width, View.Frame.Height + differenceInHeight);
此外,作为旁注,请验证 wkwebview 是否作为子视图添加到滚动视图而不是主视图。