我一直在一些应用程序上使用相同的 PDFKit 文档视图,没有任何问题。然而,在一个视图控制器上,PDFview 似乎横向移动了 16 像素的安全边距。
PDFview 设置为超级视图的宽度,检查 PDFview 大小似乎是正确的(390px)。但是 x 位置虽然声明为 0,但似乎是 16px。这会导致 pdf 文档比屏幕宽,从而导致水平滚动。
我尝试禁用安全边距等,但没有效果。 有趣的是,页面上的其他元素(视图)都不受此影响。
视图层次结构为: 垂直堆栈视图 - 由 3 个子视图组成
信息和表格视图占据相同的空间,其中一个根据分段控制而隐藏。
下面是层次结构和屏幕输出的图像,请注意 PDF 文档已移至右侧。它仍然具有屏幕的整个宽度,因此它在右侧“重叠”,导致滚动。
以前从未遇到过这个问题,似乎无法弄清楚“边距”从何而来,以及为什么只影响 PDF 视图,而不影响桌面视图。
代码:
// set PDf for Information layer in variable UIView
func pdfInfoDisplay() {
infoView.backgroundColor = UIColor.white
var pdfFull = pdfInfoObject
pdfFull.append(".pdf")
var pdfURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
pdfURL = pdfURL.appendingPathComponent(pdfFull) as URL
if let pdfDocument = PDFDocument(url: pdfURL) {
infoView.autoresizesSubviews = true
infoView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin, .flexibleLeftMargin]
infoView.displayDirection = .vertical
infoView.pageShadowsEnabled = false
infoView.autoScales = true
infoView.displayMode = .singlePageContinuous
infoView.displaysPageBreaks = true
infoView.document = pdfDocument
infoView.maxScaleFactor = 4.0
infoView.minScaleFactor = infoView.scaleFactorForSizeToFit
}
}
在 Swift 中使用 PDFkit 的简单示例:
import UIKit
import PDFKit
class ViewController: UIViewController {
// Declare PDFView
var pdfView: PDFView!
override func viewDidLoad() {
super.viewDidLoad()
// Initialize PDFView
pdfView = PDFView()
pdfView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(pdfView)
// Set constraints for PDFView
NSLayoutConstraint.activate([
pdfView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
pdfView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
pdfView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
pdfView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
])
// Load PDF Document
if let pdfURL = Bundle.main.url(forResource: "example", withExtension: "pdf"),
let pdfDocument = PDFDocument(url: pdfURL) {
pdfView.document = pdfDocument
pdfView.autoScales = true
} else {
print("Error: PDF not found or unable to load.")
}
}
}