2011-10-18 33 views
2

我尝试通过UIPopoverController内的UIWebView来打开维基手机版本的网页。问题是,无论我如何设置我的contentSizeForViewInPopover,或只是UIWebView框架,或简单地设置UIWebView.scalesPageToFit = YES。 Wiki移动版本页面的内容大小似乎比我的UIWebView大。但是如果我在iPhone上使用它,就没有这样的问题。这里是我的酥料饼的控制器代码:在UIPopoverController中的UIWebView中显示维基移动页面

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

所有代码似乎是典型的... 我想知道如果任何人都可以摆脱我一些光,非常感谢。

回答

2

哦,我发现在另一个QA中,有时如果html有一行“width = device-width”,并且你从popover控制器加载webview,这个popover控制器会自动发送设备宽度,而不是视图宽度你指定,并使你的视图丑陋和时髦。在那篇文章中,它是一个jQuery问题,并用jQuery方式解决。在我的问题中,这只是维基手机版的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; 
    } 
} 
} 

这样的事情。

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

2

这将是更有效的操纵装置宽度通过JavaScript,而不是改变HTML 它完全加载,然后再重装与修改后的HTML完整的页面。

这应该工作(并考虑如果它甚至需要改变视口宽度):

​​
3

这是AUCO答案,在这里,如果视meta标签不存在,这将是一个加强版补充:

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

以下是我们在与web视图的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);*/ 
} 
宽度注入的JavaScript漂亮格式化代码

如果需要,您可以删除try/catch。