【问题标题】:displaying Wiki mobile page in UIWebView within UIPopoverController在 UIPopoverController 内的 UIWebView 中显示 Wiki 移动页面
【发布时间】:2011-10-18 14:08:58
【问题描述】:

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

//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 的代码:

- (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;
}

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

【问题讨论】:

    标签: uiwebview


    【解决方案1】:

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

    - (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 像素

    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。

    【讨论】:

      【解决方案2】:

      哦,我在另一个 QA 中发现,有时如果 html 有一行“width=device-width”,并且您从弹出框控制器加载 webview,此弹出框控制器将自动发送设备宽度,而不是您的视图宽度指定,并使您的视图丑陋和时髦。在那篇文章中,这是一个 jQuery 问题,它通过 jQuery 方式解决。在我的问题中,这只是 wiki 移动版本中的一个 html 问题。所以我尝试了另一种方式,但类似。

      我在 webViewdidload 委托方法中简单添加了一个代码,首先将 URL html 获取为 NSString,然后使用 NSString 实例方法在加载的 html 中搜索“device-width”,并将其替换为我的视图宽度以使其成为新的NSString,然后用这个新的 NSString 加载这个页面。就是这样。

      - (void) webViewDidFinishLoad:(UIWebView *)webView
      {
      if (!alreadyReload) 
      {
          NSString *webHTML = [NSString stringWithContentsOfURL:webView.request.URL encoding:NSUTF8StringEncoding error:NULL];
          NSRange range = [webHTML rangeOfString:@"device-width"];
          if ((range.location!=NSNotFound)&&(range.length != 0)) 
          {
              webHTML = [webHTML stringByReplacingOccurrencesOfString:@"device-width" withString:@"whatever width you need" options:0 range:range];
              [webView loadHTMLString:webHTML baseURL:wikiWebView.request.URL];
              alreadyReload = YES;
          }
      }
      }
      

      类似的东西。

      顺便说一下,由于我只在wiki移动版上使用这个,html很简单,这种比较和替换很容易。如果你想在更一般的情况下使用它,你可以使用其他方式。

      【讨论】:

        【解决方案3】:

        通过 JavaScript 操作设备宽度要比在完全加载后修改 html 然后用修改后的 html 重新加载整个页面要高效得多再次。

        这应该可行(并且还要考虑是否有必要更改视口宽度):

        - (void)webViewDidFinishLoad:(UIWebView *)aWebView {
            if(aWebView.frame.size.width < aWebView.window.frame.size.width) {
                // width=device-width results in a wrong viewport dimension for webpages displayed in a popover
                NSString *jsCmd = @"var viewport = document.querySelector('meta[name=viewport]');";
                jsCmd = [jsCmd stringByAppendingFormat:@"viewport.setAttribute('content', 'width=%i, initial-scale=1.0, user-scalable=1');", (NSUInteger)aWebView.frame.size.width];
                [aWebView stringByEvaluatingJavaScriptFromString:jsCmd];
            }
            // stop network indicator
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-04-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-25
          • 1970-01-01
          相关资源
          最近更新 更多