18

我正在创建一个 PDFViewer 应用程序。我已将 PDFViewer 的 autoScale 属性设置为 true,以便视图扩展到屏幕的宽度。适用于大型 PDF 文档。但是当文档是单页文档时,页面会自动向下滚动到页尾,而不是从头开始。我只是无法理解它的根本原因。我在这里想念什么?

4

9 回答 9

8

我认为这是 PDFView 中的一个错误。

作为解决方法,我建议手动滚动顶部:

self.pdfView.document = pdfDocument;

NSPoint pt = NSMakePoint(0.0, [self.pdfView.documentView bounds].size.height);
[self.pdfView.documentView scrollPoint:pt];

错误报告器

rdar://37942090 : PDFview 在单页文档中滚动到页面底部。

于 2014-12-05T04:18:58.050 回答
7

这就是我所做的,它有点hacky

if let scrollView = pdfView.subviews.first as? UIScrollView {
    scrollView.contentOffset.y = 0.0
}
于 2019-06-05T19:12:21.727 回答
6

我建议使用此解决方案滚动到首页顶部(适用于 iOS 11.3):

if let document = pdfView.document,
   let firstPage = document.page(at: 0)
{
    let firstPageBounds = firstPage.bounds(for: pdfView.displayBox)
    pdfView.go(to: CGRect(x: 0, y: firstPageBounds.height, width: 1.0, height: 1.0), on: firstPage)
}
于 2018-05-05T07:16:57.207 回答
5

看起来该go命令需要在下一次运行时通过运行循环完成才能可靠地工作:

DispatchQueue.main.async
{
    guard let firstPage = pdfView.document?.page(at: 0) else { return }
    pdfView.go(to: CGRect(x: 0, y: Int.max, width: 0, height: 0), on: firstPage)
}

在 iOS 12 上测试

于 2019-04-16T18:43:20.953 回答
4

扩展 Petro 的答案,我在旋转页面和裁剪页面方面遇到了一些问题,但这似乎普遍适用。我实际上在旋转页面上测试了它,并在角落里添加了一个注释来验证我选择了正确的角落:

     guard let firstPage = pdfView!.document?.page(at: 0) else {
        return;
     }
     let firstPageBounds = firstPage.bounds(for: pdfView!.displayBox)
     switch (firstPage.rotation % 360) {
     case 0:
        topLeftX = firstPageBounds.minX
        topLeftY = firstPageBounds.maxY
     case 90:
        topLeftX = firstPageBounds.minX
        topLeftY = firstPageBounds.minY
     case 180:
        topLeftX = firstPageBounds.maxX
        topLeftY = firstPageBounds.minY
     case 270:
        topLeftX = firstPageBounds.maxX
        topLeftY = firstPageBounds.maxY
     default:
        print ("Invalid rotation value, not divisible by 90")
     }

     pdfView!.go(to: CGRect(x: topLeftX, y: topLeftY, width: 1.0, height: 1.0), on: firstPage)
于 2018-07-09T20:22:26.890 回答
4

我在 viewDidLoad 中加载文档,然后将其移至 viewDidAppear,它对我有用。

于 2019-07-08T08:35:23.010 回答
1

PDFPage 的旋转很重要!这是 Oded 的 Objective-C 版本:

- (void)scrollToTopOfPage:(PDFPage *)page {
    CGRect pageBounds = [page boundsForBox:self.displayBox];
    CGFloat topLeftX = 0;
    CGFloat topLeftY = 0;
    switch (page.rotation % 360) {
        case 0:
            topLeftX = CGRectGetMinX(pageBounds);
            topLeftY = CGRectGetMaxY(pageBounds);
            break;
        case 90:
            topLeftX = CGRectGetMinX(pageBounds);
            topLeftY = CGRectGetMinY(pageBounds);
            break;
        case 180:
            topLeftX = CGRectGetMaxX(pageBounds);
            topLeftY = CGRectGetMinY(pageBounds);
            break;
        case 270:
            topLeftX = CGRectGetMaxX(pageBounds);
            topLeftY = CGRectGetMaxY(pageBounds);
            break;
        default:
            break;
    }

    [self goToRect:CGRectMake(topLeftX, topLeftY, 1, 1) onPage:page];
}
于 2020-01-24T23:56:53.767 回答
1
if let document = PDFDocument(url: viewModel.url) {
        pdfView.document = document
        
        // avoid iOS autoscale issue
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [weak self] in
            self?.pdfView.autoScales = true
        }
    }

autoScales在加载文档后,我使用了变通方法来定义属性。第一页很适合放在上面。

于 2021-05-21T09:03:16.550 回答
0

对于 Objective-C 使用:

PDFPage *firstPage = [pdfView.document pageAtIndex:0];
CGRect firstPageBounds = [firstPage boundsForBox:pdfView.displayBox];
[pdfView goToRect:CGRectMake(0, firstPageBounds.size.height, 1, 1) onPage:firstPage];
于 2019-08-23T09:39:40.803 回答