如何在横向显示具有正确缩放比例的 UIWebView
我创建 UIWebView 并设置自动旋转,如下所示:
- (void)loadView
{
self.view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)] autorelease];
UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
webview.delegate = self;
webview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_darkgreen_320x312.png"]];
webview.dataDetectorTypes = UIDataDetectorTypeLink;
webview.multipleTouchEnabled = YES;
webview.scalesPageToFit = YES;
webview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[webview loadHTMLString:@"<meta name=\"viewport\" content=\"width=320\">content ..." baseURL:nil];
[self.view addSubview:webview];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
当我在横向旋转它而不缩放时,它完美地工作。两次旋转时都会显示全屏。
问题是,如果我在纵向模式下缩小或稍微放大,然后在横向旋转它,webview 仍然以与纵向模式相同的大小进行缩放,并且它有黑色空间,如图所示 > http://cl.ly/1o3c4712053C3T2C2H2z
它的横向宽度是正确的,480px(你可以看到它的滚动条打开了屏幕最右侧)。我不明白为什么 webview 不显示全屏。
我做错了什么? 有什么办法可以解决这个问题吗? 请帮忙。
提前致谢。
I create UIWebView and set autorotate like this:
- (void)loadView
{
self.view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)] autorelease];
UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
webview.delegate = self;
webview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_darkgreen_320x312.png"]];
webview.dataDetectorTypes = UIDataDetectorTypeLink;
webview.multipleTouchEnabled = YES;
webview.scalesPageToFit = YES;
webview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[webview loadHTMLString:@"<meta name=\"viewport\" content=\"width=320\">content ..." baseURL:nil];
[self.view addSubview:webview];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
It works perfectly when I rotate it in landscape without zooming. It will display full screen on both rotation.
The problem is if I zoom-out or a little bit zoom-in in portrait mode and then rotate it in landscape, webview still zoom with the same size of portrait mode and it has black space like in picture > http://cl.ly/1o3c4712053C3T2C2H2z
It's width in landscape is correct, 480px (you can see its scrollbar is on most right of screen). I don't understand why webview don't display full screen.
What I do wrong?
Is there any way to fix this?
Please help.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了达到您的效果,您需要在旋转时将 ZoomScale 设置为 0。请在此处查看我的完整答案: UIWebView 内容未调整旋转后到新框架
In order to achieve your effect you need to set the zoomScale to 0 on rotation. See my full answer here: UIWebView content not adjusted to new frame after rotation