ios - 设备方向改变时调整 PDF View 大小

标签 ios iphone swift pdfkit

我的应用程序中有一个 ViewController,允许用户打开 PDF。该应用程序支持设备的横向和纵向方向。当用户在打开 PDF 后更改设备方向时,我遇到了问题。 PDF 有两页: 第 1 页 - 全粉红和 第 2 页 - 全紫色

打开 PDF 并且用户更改设备方向后,我遇到了框架问题。如果以纵向打开 PDF,一旦设备的排列方式更改为横向,pdf 只会填满屏幕的一半: Portrait Orientation after being opened in a portrait orientation Landscape Orientation after being opened in portrait orientation

同样,如果 PDF 以横向打开,则当设备方向为纵向时,pdf 将仅显示在屏幕的一半上: Lanscape Orientation after being opened in a landscape Orientation Portrait Orientation after being opened in a landscape Orientation

这是我的代码:

import UIKit
import PDFKit

class PDFfun: UIViewController {

    var pdfView = PDFView()

override func viewDidLoad() {
    super.viewDidLoad()

    pdfView = PDFView(frame: self.view.frame)

    if let documentURL = Bundle.main.url(forResource: "Blank", withExtension: "pdf"),
        let document = PDFDocument(url: documentURL),
        let _ = document.page(at: 0) {

        pdfView.document = document
        pdfView.autoScales = true
        pdfView.backgroundColor = UIColor.lightGray
        pdfView.autoScales = true

     }
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        if UIDevice.current.orientation.isLandscape {
            print("landscape")
            pdfView = PDFView(frame: view.frame)
            pdfView.autoScales = true
        } else {
            print("portait")
            pdfView = PDFView(frame: view.frame)
            pdfView.autoScales = true
        }
    }
}

我厌倦了添加 override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) 解决了我的问题,但我发现没有任何变化。

最佳答案

最好的方法是将布局约束添加到 pdfView。例如在你的 viewDidLoad 中:

   pdfView.translatesAutoresizingMaskIntoConstraints = false
   view.addSubview(pdfView)     
   view.addConstraints([
        NSLayoutConstraint(item: pdfView, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0),
        NSLayoutConstraint(item: pdfView, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0),
        NSLayoutConstraint(item: pdfView, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0),
        NSLayoutConstraint(item: pdfView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0)
        ])

如果您不想使用 layoutConstraint,您也可以在 viewDidLayoutSubviews 方法中为您的 pdfView 设置框架:

    override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

         pdfView.frame = view.frame
    }

并且不要忘记删除 viewWillTransition 方法中的代码。

关于ios - 设备方向改变时调整 PDF View 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54149250/

相关文章:

SwiftUI - 如何使 NavigationView 不影响几何读取器的位置

ios - 在 swift 中添加 Nsattributed 字符串和字符串

ios - XCode:为什么我的核心数据更改只有在重新启动模拟器后才会出现?

ios - 出现键盘时向上移动 TextField/View,但前提是隐藏了 Text Field

iphone - 如何更改...更多导航的标题

ios - 应用程序卡在核心数据初始化。怎么修?

ios - 如何使用 Swift 删除 Parse.com 中的列

ios - iMessage 扩展和 CoreData

iphone - NSString - 截断小数点后的所有内容

iphone - 尝试创建函数来计算选中单元格的数量并从另一个函数调用它