在 UIPopoverController 内的 UIWebView 中显示 Wiki 移动页面 [英] displaying Wiki mobile page in UIWebView within UIPopoverController

查看:17
本文介绍了在 UIPopoverController 内的 UIWebView 中显示 Wiki 移动页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过 UIPopoverController 中的 UIWebView 打开 wiki 移动版网页.问题是,无论我如何设置 contentSizeForViewInPopover,或者只是 UIWebView 框架,或者只是设置 UIWebView.scalesPageToFit = YES.Wiki 移动版页面内容大小似乎比我的 UIWebView 大.但是如果我在 iPhone 上使用它,就没有这样的问题.这是我的弹出框控制器代码:

I try to open wiki mobile version webpage by a UIWebView within a UIPopoverController. the problem is, not matter how I set my contentSizeForViewInPopover, or just UIWebView frame, or simply set UIWebView.scalesPageToFit = YES. the Wiki mobile version page content size seem to larger than my UIWebView. But if I use it on iPhone, there's no such problem. here's my code for popover controller:

//create a UIWebView UIViewController first
WikiViewController *addView = [[WikiViewController alloc] init];
addView.contentSizeForViewInPopover = CGSizeMake(320.0, 480.0f);

//then create my UIPopoverController
popover = [[UIPopoverController alloc] initWithContentViewController:addView];
popover.delegate = self;
[addView release];

//then get the popover rect
CGPoint pointforPop = [self.mapView convertCoordinate:selectAnnotationCord 
                                        toPointToView:self.mapView];
CGRect askRect = CGRectMake((int)pointforPop.x, (int)pointforPop.y+10, 1.0, 1.0);
[popover presentPopoverFromRect:askRect 
                         inView:self.mapView 
       permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
[self.mapView deselectAnnotation:annotation animated:YES];

这是我创建 UIWebView 的代码:

and this is my code on creating UIWebView:

- (void)viewDidLoad
{
 wikiWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
 wikiWebView.scalesPageToFit = YES;
 //or No, doesn't matter, it all get larger than this
 wikiWebView.delegate = self;
 self.view = wikiWebView;
}

所有代码似乎都很典型...不知道有没有人可以给我一些启发,非常感谢.

all code seem to be typical... I wonder if anyone can shed me some light, thank you so much.

推荐答案

这是 auco answer 的增强版本,如果 viewport 元标记不存在,则会添加:

This is an enhanced version of auco answer, where if the viewport meta tag is not present it will be added:

- (void)webViewDidFinishLoad:(UIWebView*)webView
{
    int webviewWidth = (NSUInteger)webView.frame.size.width;

    if (!webView.loading) {

        NSString *jsCmd = [NSString stringWithFormat:@"try {var viewport = document.querySelector('meta[name=viewport]');if (viewport != null) {viewport.setAttribute('content','width=%ipx, initial-scale=1.0, user-scalable=1');} else {var viewPortTag=document.createElement('meta');viewPortTag.id='viewport';viewPortTag.name = 'viewport';viewPortTag.content = 'width=%ipx, initial-scale=1.0, user-scalable=1';document.getElementsByTagName('head')[0].appendChild(viewPortTag);}} catch (e) {/*alert(e);*/}", webviewWidth, webviewWidth];

        [webView stringByEvaluatingJavaScriptFromString:jsCmd];
    }
}

这是我们在 WebView 中注入的 Javascript 格式漂亮的代码,宽度为 320 像素

Here is the Javascript pretty formatted code we are injecting in the WebView with a width of 320px

try {
    var viewport = document.querySelector('meta[name=viewport]');
    if (viewport != null) {
        viewport.setAttribute('content',
                'width=320px, initial-scale=1.0, user-scalable=1');
    } else {
        var viewPortTag = document.createElement('meta');
        viewPortTag.id = 'viewport';
        viewPortTag.name = 'viewport';
        viewPortTag.content = 'width=320px,initial-scale=1.0, user-scalable=1';
        document.getElementsByTagName('head')[0].appendChild(viewPortTag);
    }
} catch (e) {
    /*alert(e);*/
} 

您可以根据需要删除 try/catch.

you can remove the try/catch if you want.

这篇关于在 UIPopoverController 内的 UIWebView 中显示 Wiki 移动页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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