在 UIPopoverController 内的 UIWebView 中显示 Wiki 移动页面
我尝试通过 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;
}
所有代码似乎都是典型的...... 我想知道是否有人可以给我一些启发,非常感谢。
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];
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是auco答案的增强版本,如果视口元标记不存在,则会添加它:
这是我们在宽度为320px的WebView中注入的Javascript格式漂亮的代码,
您可以删除try/catch,如果您想。
This is an enhanced version of auco answer, where if the viewport meta tag is not present it will be added:
Here is the Javascript pretty formatted code we are injecting in the WebView with a width of 320px
you can remove the try/catch if you want.
哦,我在另一个QA中发现,有时如果html中有一行“width=device-width”,并且您从popover控制器加载webview,则该popover控制器将自动发送设备宽度,而不是您指定的视图宽度,并且让你的观点变得丑陋和时髦。在那篇文章中,这是一个 jQuery 问题,它用 jQuery 方式解决了。在我的问题中,这只是 wiki 移动版本中的 html 问题。所以我尝试另一种方式,但类似。
我简单地在 webViewdidload 委托方法中添加代码,首先将 URL html 获取到 NSString 中,然后使用 NSString 实例方法在加载的 html 中搜索“device-width”,并将其替换为我的视图宽度以使其成为新的 NSString,然后使用这个新的 NSString 加载此页面。就是这样。
像这样的东西。
顺便说一句,由于我只在 wiki 移动版本上使用它,所以 html 很简单,这种比较和替换也很容易。如果你想在更一般的情况下使用它,你可以使用其他方式。
oh, i found in another QA that sometimes if html got a line "width=device-width", and you load a webview from popover controller, this popover controller will automatically send out device-width, not the view width you specified, and make your view ugly and funky. in that post it is a jQuery issue, and it solved with a jQuery way. In my problem, it is just a html issue in wiki mobile version. so I try another way, but similar.
I simple add a code in webViewdidload delegate method, first get URL html into a NSString, then use NSString instance method to search for "device-width" in loaded html, and replace it with my view width to make it a new NSString, then load this page with this new NSString. that's it.
something like this.
by the way, since I only use this on wiki mobile version, the html is simple and this kind of compare and replace is pretty easy. if you wanna use it in a more general case, you might use other way.
通过 JavaScript 操作设备宽度比在 html 完全加载后更改 html,然后再次使用修改后的 html 重新加载整个页面要高效得多。
这应该可行(并且还要考虑是否有必要更改视口宽度):
It would be much more efficient to manipulate the device-width via JavaScript rather than altering the html after it has fully loaded and then reloading the full page with modified html again.
This should work (and also consider if it's even necessary to change the viewport width):