Javascript 使用 UIWebView 时出现奇怪的填充/边距

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2442727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:21:58  来源:igfitidea点击:

Strange padding/margin when using UIWebView

javascriptiphoneobjective-cuiviewuiwebview

提问by Nicsoft

I am creating an app that is having a UIWebView which contains an advert. The size of the view is the same as the advert (image) itself. Still, there is a white margin/padding of some kind above and to the left of the image, inside the UIWebView. Check out the linked image:

我正在创建一个包含广告的 UIWebView 的应用程序。视图的大小与广告(图像)本身相同。尽管如此,在 UIWebView 内部的图像上方和左侧仍有某种白色边距/填充。查看链接的图像:

alt text

替代文字

Actually, the image is pushed down and to the right due to this padding also.

实际上,由于这种填充,图像也被向下和向右推。

Any idea how I should remove the white padding?

知道我应该如何去除白色填充吗?

Thanks in advance!

提前致谢!

回答by Sixten Otto

Basically, all browsers add in that whitespace around the edges of the page to be backwards-compatible with like Netscape 1. In the HTML you're loading into the web view, you can use CSS to remove that:

基本上,所有浏览器都会在页面边缘添加空白,以便与 Netscape 1 向后兼容。在您加载到 Web 视图的 HTML 中,您可以使用 CSS 来删除它:

body { margin: 0; padding: 0; }

If you're not loading HTML into your web view, but just the direct URL for an image file, I suggest either a) wrapping that in some basic HTML (head, body, an imgtag), or b) downloading the image yourself (say with NSURLConnection), and displaying it directly in a UIImageView.

如果您没有将 HTML 加载到您的 Web 视图中,而只是加载图像文件的直接 URL,我建议您要么 a) 将其包装在一些基本的 HTML(头部、正文、img标签)中,要么 b) 自己下载图像(用NSURLConnection)说,并将其直接显示在UIImageView.

回答by thenextmillionaire

I know this is an old post, but just in case anyone is having the same issues. I was able to fix the same issue by adding the following code into the webViewDidFinishLoad delegate method.

我知道这是一个旧帖子,但以防万一有人遇到同样的问题。通过将以下代码添加到 webViewDidFinishLoad 委托方法中,我能够解决相同的问题。

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *padding = @"document.body.style.margin='0';document.body.style.padding = '0'";
    [webView stringByEvaluatingJavaScriptFromString:padding];
}

Look at the first banner with the above code added. And the bottom one without the code.

查看添加了上述代码的第一个横幅。而底部没有代码。

enter image description here

在此处输入图片说明

回答by Ser Pounce

As an ammendment to @thenextmillionair's brilliant answer, put the code in:

作为对@thenextmillionair 精彩答案的修正,将代码放入:

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    NSString *padding = @"document.body.style.margin='0';document.body.style.padding = '0'";
    [webView stringByEvaluatingJavaScriptFromString:padding];
}

as opposed to webViewDidFinishLoad. When the code is in webViewDidFinishLoad, you see the UIWebViewreposition itself after appearing on screen with the padding. By putting it in webViewDidStartLoad, you eliminate this ugliness.

webViewDidFinishLoad. 当代码在 中时webViewDidFinishLoad,您会UIWebView在带有填充的屏幕上看到重新定位本身。把它webViewDidStartLoad放进去,你就消除了这种丑陋。