UIWebView自动旋转损坏Web视图内容(屏幕截图) [英] UIWebView auto rotation corrupts web view content (screenshots inside)

查看:317
本文介绍了UIWebView自动旋转损坏Web视图内容(屏幕截图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIWebView自动旋转的问题。

的网页视图straches正好,但内容不是。

你可以看到在底部的屏幕截图有一个黑边,我知道这是webview,因为我可以看到滚动时,我滚动。



视图控制器有:

   - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
return YES;
}

webview有完整的自动调整大小掩码,视图是自动调整大小。



我尝试了一堆stackoverflow和谷歌的技巧,但没有一个工作。

这只发生在一些网站,但如果我打开



编辑:有问题的网站的示例: http://m.calcalist.co.il



WebView LoadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSURLRequest]] { @http://m.calcalist.co.il]]];

scalesPageToFit = YES; //不帮助

谢谢!





解决方案

将Web视图的 scalesPageToFit 属性设置为 YES



如果您自己生成HTML,则还可以设置视口元标记。



已设置视口,您可以将您的viewcontroller设置为web视图的委托并更改为:

   - (void) webViewDidFinishLoad:(UIWebView *)webView {

NSString * javaScript = @var metatags = document.getElementsByTagName('meta'); \
for(cnt = 0; cnt< metatags .length; cnt ++){\
var element = metatags [cnt]; \
if(element.getAttribute('name')=='viewport'){\
element .setAttribute('content','width = device-width; user-scalable = yes'); \
} \
};
[webView stringByEvaluatingJavaScriptFromString:javaScript];
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {

if([self interfaceOrientation] == UIInterfaceOrientationPortrait)
[webView stringByEvaluatingJavaScriptFromString:@document.body.style.zoom = 1.0;];
else
[webView stringByEvaluatingJavaScriptFromString:@document.body.style.zoom = 1.5;];
}


i'm having a problem with UIWebView auto-rotating.
the web view straches alright but the content is not.
as you can see in the bottom screenshot there is a black margin, and i know it's the webview since i can see the scrollers when i scroll.

the view controller has:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
    return YES;
}

the webview has full auto resizing mask, and the view is auto-resizing subviews.

i tried a bunch of tricks from stackoverflow and google but none of them worked.
this happens only on some sites, but if i open the problematic sites in mobile safari or even ios opera browser, it rotates correctly.

EDIT: an example of a problematic site: http://m.calcalist.co.il

the webview is set in the interface builder and loading with this code:

[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.calcalist.co.il"]]];

scalesPageToFit = YES; // doesn't help

Thanks!

解决方案

You can set the web view's scalesPageToFit property to YES.

If you are generating the HTML yourself, then you can also set the viewport meta tag.

If the page has already set the viewport, you can set your viewcontroller to be the web view's delegate and change it like this:

- (void)webViewDidFinishLoad:(UIWebView *)webView {

    NSString *javaScript = @"var metatags = document.getElementsByTagName('meta'); \
    for(cnt = 0; cnt < metatags.length; cnt++) { \
        var element = metatags[cnt]; \
        if(element.getAttribute('name') == 'viewport') { \
            element.setAttribute('content','width = device-width; user-scalable = yes'); \
        } \
    }";
    [webView stringByEvaluatingJavaScriptFromString:javaScript];
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {

    if ([self interfaceOrientation] == UIInterfaceOrientationPortrait)
        [webView stringByEvaluatingJavaScriptFromString:@"document.body.style.zoom = 1.0;"];
    else
        [webView stringByEvaluatingJavaScriptFromString:@"document.body.style.zoom = 1.5;"];
}

这篇关于UIWebView自动旋转损坏Web视图内容(屏幕截图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆