如何使用PDFKit禁用PDFView
中的滚动反弹?
显示 PDF 的视图没有滚动跳出选项。
这是我的代码:
if let path = Bundle.main.path(forResource: pdfObject, ofType: "pdf") {
let url = URL(fileURLWithPath: path)
if let pdfDocument = PDFDocument(url: url) {
pdfView.autoresizesSubviews = true
pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight,
.flexibleTopMargin, .flexibleLeftMargin]
pdfView.autoScales = true
pdfView.displaysPageBreaks = true
pdfView.displayDirection = .vertical
pdfView.displayMode = .singlePageContinuous
pdfView.document = pdfDocument
pdfView.maxScaleFactor = 4.0
pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
}
}
提前感谢(这可能是一个非常简单的问题!
不幸的是,没有导出的 API 来设置PDFView
所需的弹跳行为。
话虽如此,您现在可以(安全地)利用PDFView
实现细节来破解它:
extension PDFView {
/// Disables the PDFView default bouncing behavior.
func disableBouncing() {
for subview in subviews {
if let scrollView = subview as? UIScrollView {
scrollView.bounces = false
return
}
}
print("PDFView.disableBouncing: FAILED!")
}
}
然后在代码中像这样使用它:
pdfView.disableBouncing()
警告。请记住,此类解决方案可能会在未来的iOS版本中中断。不过,请放心,您的应用程序不会因此而崩溃(您根本不会禁用弹跳行为)。