UIWebView在loadRequest上调整大小

时间:2015-12-11 11:09:46

标签: ios webview xamarin uiwebview xamarin.ios

我有一个UIWebView,其内容可以缩放以适应。然后调整webview的大小以适应内容(见下文),并按预期工作。

webview还附加了一个滑动手势来加载新页面 - 当loadRequest执行webview下一页时,会有一个短暂的闪烁回到webview的初始宽度。我已确认使用NSTimerUILabel来更改框架以显示框架的宽度。

以下是重新调整大小的代码:

string bookHeightS = bookWebview.EvaluateJavascript ("document.getElementsByTagName('body')[0].scrollHeight");
string bookWidthS = bookWebview.EvaluateJavascript ("document.getElementsByTagName('body')[0].offsetWidth");

nfloat bookHeight = 0;
nfloat.TryParse (bookHeightS, out bookHeight);

nfloat bookWidth = 0;
nfloat.TryParse (bookWidthS, out bookWidth);

nfloat scaling = bookWebview.Frame.Height / bookHeight;

nfloat initialWidth = bookWebview.Frame.Width;
nfloat newWidth = scaling * bookWidth;

CGRect frame = new CGRect (bookWebview.Frame.Location, new CGSize (newWidth, bookWebview.Frame.Height));


if (frame.Width != bookWebview.Frame.Width) {
    bookWebview.Frame = frame;
}

此代码由webview上的LoadFinished代表调用。

我已尝试设置webview的滚动视图框架和webview ContentSize的大小。

导致这种闪烁的原因是什么?

flickering webview

1 个答案:

答案 0 :(得分:1)

在这里调整webview框架的大小并不完全正确。 一旦加载请求 [uiwebview loadRequest:] 成功完成,LoadFinished处理程序就会触发,并不一定意味着HTML内容(DOM)的执行已经完成。 这里的一些注释可能会有所帮助:

1 - 请确保您在HTML设计中包含了一个视图端口标记,以保持填充方面。 (在头脑中)

<meta name="viewport" content="width=device-width, user-scalable=no">

2 - 检查UIWebView内容模式(默认为scale to fit),如果需要,还要检查supress incremental Rendering(取决于您的设计)

3 - 实际上没有办法捕获Dom元素在HTML中加载和渲染的时间。也许你可以在那里包含一些加载器,超时(记得在页面卸载时清除它)

4 - 如果你的设计有一些CSS标准,你可能需要考虑100%宽度的元素来填充页面。

5 - 如果您使用自动布局,只需修复UIWebview的框架,而不是在每次滑动时处理WebViewFrame。

这就是我所能想到的,祝你好运。