【问题标题】:How can I display a PDF file full screen on UIWebview entire view?如何在 UIWebview 整个视图上全屏显示 PDF 文件?
【发布时间】:2013-09-03 12:34:58
【问题描述】:

我正在尝试使用设备的整个屏幕在 UIWebview 中显示 pdf。我有一个在 iPad 上运行的 xcode 应用程序。我有一个滚动视图来滚动 pdf 页面 UIWebview 是 uiscrollview 的子视图。 PDF是从互联网上下载的,没有问题, 目前,PDF 在显示时仅在纵向模式下使用 uiwebview 上屏幕的最上半部分。我需要它来跨越 UIWebview 的整个视图。

NSString *strPageURL = [dictPage valueForKey:@"link"];

        strPageURL = [strPageURL stringByReplacingOccurrencesOfString:@"\n" withString:@""];
        strPageURL = [strPageURL stringByReplacingOccurrencesOfString:@"\t" withString:@""];
        strPageURL = [strPageURL stringByReplacingOccurrencesOfString:@" " withString:@""];

        NSString* strFileName = [strPageURL lastPathComponent];
                NSString *strDestFile = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@",strFileName]];

        CGRect rect = CGRectMake(xPos, 0, scrollView.frame.size.width, scrollView.frame.size.height);     
        UIWebView * webView = [[UIWebView alloc] initWithFrame:rect];
        webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
        [webView setContentMode:UIViewContentModeScaleAspectFit];
        [webView setScalesPageToFit:YES];
        webView.backgroundColor = [UIColor whiteColor];
        webView.tag=3000+i;

        BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:strDestFile];

        if(fileExists)
        {

            NSURL *url = [[NSURL alloc] initFileURLWithPath:strDestFile];

            [webView loadRequest:[NSURLRequest requestWithURL:url]];
            [webView setDelegate:self];
            [scrollView addSubview:webView];

        }

【问题讨论】:

    标签: ios objective-c pdf uiwebview


    【解决方案1】:

    UIWebView 内置了UIScrollView,因此您实际上不需要手动管理它。

    您只需将 PDF 加载到 UIWebView 中,它就会自动以正确的形式显示。

    您可以通过您的 URL 请求执行此操作:

    //first create the webview
    UIWebView *theWebView = [[UIWebView alloc] initWithFrame:[self.view bounds]];
    [theWebView setContentMode:UIViewContentModeScaleAspectFit];
    [theWebView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
    [theWebView setScalesPageToFit:YES];
    
    //then load the correct file
    NSString *filePath = [[NSBundle mainBundle] pathForResource:YOURFILENAME ofType:YOURFILETYPE];
    NSURL *filePathURL = [NSURL fileURLWithPath:filePath];        
    [theWebView loadRequest:[NSURLRequest requestWithURL:filePathURL]];
    
    //then add to your view
    [self.view addSubview:theWebView];
    

    YOURFILENAMEYOURFILETYPE显然要换成你自己的代码。

    如果它在横屏时仍然没有调整大小,您可能需要在自动旋转方法中添加一些代码来手动调整大小 - 尽管 setAutoresizingMask 行应该会为您处理它。

    【讨论】:

    • sry,我需要显示 pdf 文件 uiwebview 肖像全屏,我已附上截图供您参考
    • 这正是我的解决方案所做的。 [self.view bounds] 将 webview 设置为完整视图的大小。或者,您可以使用 [[UIScreen mainScreen] bounds] 作为框架。
    • 我已经发布了我的代码,请检查一下我做错了什么?我该怎么做?
    • 我想将 UIWebview 添加为 UIScrollview 的子视图,因为我从提要获取 pdf 文件(单页)
    • UIScrollView 的作用是什么?
    【解决方案2】:

    我认为您不必使用 ScrollView。只需添加网页视图。单击 XCode Storyboard(或 xib 文件)中的 Web 视图。在右侧栏中,转到“属性检查器”(参见随附的屏幕截图)。选中“缩放页面以适合”。

    【讨论】:

    • 我正在使用 UIScrollview。我已经设置了 [webView setScalesPageToFit:YES];
    猜你喜欢
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    • 2016-08-10
    • 2015-04-29
    • 1970-01-01
    • 2013-01-09
    • 2012-01-29
    相关资源
    最近更新 更多