在UIWebView上启用缩放/缩放

时间:2011-08-20 20:43:47

标签: pdf uiwebview

我有一个带有pdf文件的UIWebView。它工作正常。但是如何启用缩放pdf文件呢?

6 个答案:

答案 0 :(得分:228)

确保选中“Scales page to fit”

答案 1 :(得分:45)

您可以通过编程方式使用webView.scalesPageToFit=YES;

如果您在xib中使用而不仅仅是click the check box "Scaling" scales Page to fit

答案 2 :(得分:27)

  

此逻辑用于缩放UIWebView,无需在UIScrollView上添加UIWebView

webView.scalesPageToFit = YES;唯一的问题是,它会改变字体大小的初始内容,但我找到了其他选项

<UIWebViewDelegate, UIScrollViewDelegate>添加到 .h文件

创建UIWebView.

self.mWebview = [[UIWebView alloc] init];
self.mWebview.delegate = self; /// set delegate method of UIWebView
self.mWebview.frame = CGRectMake(0, 35, self.view.bounds.size.width, self.view.bounds.size.height - 80); // set frame whatever you want..
[self.mWebview setOpaque:NO];
self.mWebview.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.mWebview];

加载HTML文件/内容。

NSString* htmlString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"File Name"ofType:@"html"] encoding:NSUTF8StringEncoding error:nil];
[self.mWebview loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];


#pragma mark -
#pragma mark - Webview Delegate Methods

- (void) webViewDidFinishLoad:(UIWebView *)webView
{
    webView.scrollView.delegate = self; // set delegate method of UISrollView
    webView.scrollView.maximumZoomScale = 20; // set as you want.
    webView.scrollView.minimumZoomScale = 1; // set as you want.

    //// Below two line is for iOS 6, If your app only supported iOS 7 then no need to write this.
    webView.scrollView.zoomScale = 2;
    webView.scrollView.zoomScale = 1;
}

#pragma mark -
#pragma mark - UIScrollView Delegate Methods

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
    self.mWebview.scrollView.maximumZoomScale = 20; // set similar to previous.
}
  

注意:我必须在Mac OS X上测试 - 使用Xcode 5.1.1和iOS 6.1及更高版本的10.9.3。

我希望这会对你有所帮助。 :)

答案 3 :(得分:22)

我知道这个问题很老了,但是对于每个使用故事板的人来说,更喜欢这里的视觉答案。只需在WebView的Attributes Inspector中选中此框:

enter image description here

答案 4 :(得分:13)

如果您想通过编程方式进行操作,请使用

webView.scalesPageToFit = true; 

如果您正在使用故事板,那么您必须勾选复选框&#34; Scaling&#34;缩放页面以适应 enter image description here

答案 5 :(得分:5)

你必须设置scalesPageToFit = YES,以便在UIWebView上进行任何捏合和缩放。它适合我。