我有一个webview,它是层次结构中的顶层窗口,已声明如下所示.但是,它不会缩放页面以适应.尽管scalesPageToFit属性设置为YES,页面仍然是左上对齐,但未缩放.任何帮助将不胜感激.
webLookupView = [[UIWebView alloc] initWithFrame:CGRectMake(16, 63, 289, 327)]; webLookupView.backgroundColor = [UIColor lightGrayColor]; webLookupView.dataDetectorTypes = UIDataDetectorTypeAll; webLookupView.scalesPageToFit = YES;
Confused Vor.. 36
iOS5提供了更好的解决方案
从webViewDidFinishLoad中调用它
- (void)zoomToFit { if ([theWebView respondsToSelector:@selector(scrollView)]) { UIScrollView *scrollView = [theWebView scrollView]; float zoom = theWebView.bounds.size.width / scrollView.contentSize.width; scrollView.minimumZoomScale = zoom; [scrollView setZoomScale:zoom animated:YES]; } }
这很棒,但在它缩放后,它会弹回到之前的大小.有任何想法吗? (2认同)
RunLoop.. 29
我后来发现有些网页是正确缩放的,但有些则没有.对于未正确缩放的网页,可以使用javascript控制缩放,如下所示:
NSString *jsCommand = [NSString stringWithFormat:@"document.body.style.zoom = 1.5;"]; [webLookupView stringByEvaluatingJavaScriptFromString:jsCommand];
vualoaithu.. 22
您可以使用以下代码:
self.webView.scalesPageToFit = YES; self.webView.contentMode = UIViewContentModeScaleAspectFit;
在- (void)webViewDidFinishLoad:(UIWebView *)webView method
或viewDidLoad
iOS5提供了更好的解决方案
从webViewDidFinishLoad中调用它
- (void)zoomToFit { if ([theWebView respondsToSelector:@selector(scrollView)]) { UIScrollView *scrollView = [theWebView scrollView]; float zoom = theWebView.bounds.size.width / scrollView.contentSize.width; scrollView.minimumZoomScale = zoom; [scrollView setZoomScale:zoom animated:YES]; } }
我后来发现有些网页是正确缩放的,但有些则没有.对于未正确缩放的网页,可以使用javascript控制缩放,如下所示:
NSString *jsCommand = [NSString stringWithFormat:@"document.body.style.zoom = 1.5;"]; [webLookupView stringByEvaluatingJavaScriptFromString:jsCommand];
您可以使用以下代码:
self.webView.scalesPageToFit = YES; self.webView.contentMode = UIViewContentModeScaleAspectFit;
在- (void)webViewDidFinishLoad:(UIWebView *)webView method
或viewDidLoad
如果您有权访问正在加载的html,则可以在标题中删除以下元标记:
self.webView.scalesPageToFit = true self.webView.contentMode = UIViewContentMode.ScaleAspectFit