- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
注意:这个问题暂时还没有解决;标记的答案提供了一个很好的解决方法 - 它可以在应用程序仍处于打开状态时工作。 仍然欢迎回答!
背景
我目前正在开发一个由全屏 PDFView
组成的应用程序,我希望程序在关闭 View 之前记住文档中的位置,以便用户可以从哪里开始他们离开了。
实现
应用程序的简化版本可以理解为使用 PDFKit.PDFView
的 PDF 查看器。 Storyboard包含一个连接到 PDFView 类的 UIView,DocumentView
(符合 UIViewController
)。 View 通过以下过程初始化:
在viewDidLoad
中:
let PDF: PDFDocument = GetPDFFromServer()
DocumentView.document = PDF!
DocumentView.autoScales = true
... (other settings)
// Set PDF Destination
let Destination: PDFDestination = GetStoredDestination()
// Code with issues
DocumentView.go(to: Destination)
在viewWillDisappear
中:
StoreDestination(DocumentView.currentDestination)
问题与测试
我意识到代码没有按预期工作; View 不会返回到之前的位置。
通过调试,我意识到这可能是由于 DocumentView.go(to destination: PDFDestination)
和 DocumentView.currentDestination
的行为不一致造成的。
为确保该错误不是由存储位置时的错误引入的,以下代码用于验证问题,并带有多页文档:
在viewDidLoad
Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { _ in
DispatchQueue.main.async {
self.DocumentView.go(to:self.DocumentView.currentDestination!)
}
})
预期和观察到的行为
预期:文档的位置不应更改 - 代码每 1 秒转到其当前目的地,这应该没有任何影响。因为“currentDestination”应该是“文档的当前目的地,根据文档”)
观察到:执行后,页面会自发地向下滚动一个固定的偏移量。
在 iPadOS 14.5 模拟器和 iPadOS 15 iPad Air(第 4 代)上观察到了相同的结果。
可能出了什么问题?
如果有人能提供帮助,那就太好了。
干杯,林肯
这个问题最初发表于 Apple Developer Forum一个多星期前;一个多星期都没有收到任何回复,所以我想我可以在 StackOverflow 上碰碰运气 <3
最佳答案
我在这种情况下尝试了 PDFView.go()
,我设法让它在某些情况下工作,但发现它在其他一些情况下失败,例如缩放文档、改变方向。
所以回到你想要实现的目标,
I'm currently developing an app that consists of a full-screenPDFView, and I want the program to remember the position in thedocument before the view is dismissed so the user can pick up wherethey've left.
这可以通过不同的方法来完成。使用这种方法,您需要始终保留对您创建的 PDFView
的引用。如果之前的 pdf 需要再次加载,那么你将 PDFView
实例原样传递给 viewController
。否则,您将新的 pdf 加载到 PDFView
实例并将其传递给 viewController
。
DocumentViewController
在初始化时获取 PDFView
。
import UIKit
import PDFKit
protocol DocumentViewControllerDelegate: AnyObject {
func needsContinuePDF(continuePDF: Bool)
}
class DocumentViewController: UIViewController {
var pdfView: PDFView!
weak var delegate: DocumentViewControllerDelegate!
init(pdfView: PDFView, delegate: DocumentViewControllerDelegate){
self.pdfView = pdfView
self.delegate = delegate
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func loadView() {
super.loadView()
self.view.backgroundColor = .white
view.addSubview(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)
])
}
override func viewWillDisappear(_ animated: Bool) {
delegate.needsContinuePDF(continuePDF: true)
}
}
您可以像下面这样初始化 DocumentViewController
。 MainViewController
负责初始化 PDFView
。
import UIKit
import PDFKit
class MainViewController: UIViewController {
var pdfView: PDFView = PDFView()
var continuePreviousPDF = false
let button = UIButton(frame: .zero)
override func loadView() {
super.loadView()
button.setTitle("View PDF", for: .normal)
button.setTitleColor(.white, for: .normal)
button.backgroundColor = .systemBlue
button.addTarget(self, action: #selector(openDocumentView(_:)), for: .touchUpInside)
self.view.backgroundColor = .systemGray5
self.view.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
button.widthAnchor.constraint(equalToConstant: 100),
button.heightAnchor.constraint(equalToConstant: 50),
button.centerXAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.centerXAnchor),
button.centerYAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.centerYAnchor),
])
}
@objc func openDocumentView(_ sender: UIButton) {
//open a nee PDF if not continue previous one
if !self.continuePreviousPDF {
pdfView.autoScales = true
pdfView.displayMode = .singlePageContinuous
pdfView.translatesAutoresizingMaskIntoConstraints = false
guard let path = Bundle.main.url(forResource: "sample copy", withExtension: "pdf") else { return }
if let document = PDFDocument(url: path) {
pdfView.document = document
}
}
let documentViewController = DocumentViewController(pdfView: pdfView, delegate: self)
self.present(documentViewController, animated: true, completion: nil)
}
}
extension MainViewController: DocumentViewControllerDelegate {
func needsContinuePDF(continuePDF: Bool) {
self.continuePreviousPDF = continuePDF
}
}
关于ios - Swift PDFKit : Inconsistent behaviour with PDFView. currentDestination 和 PDFView.go(到目的地:PDFDestination),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69308467/
我创建了 iPhone 应用程序,在其中使用 fastpdfkit 显示 PDF 并播放音频和视频文件。 现在我也想为 Android 制作相同的应用程序。有没有适用于 Android 的套件可以让我
我安装了 pdfkit,甚至还安装了 wkhtmltopdf,但是每次我尝试创建 pdf 时都会收到以下错误。 PDFKit::NoExecutableError No wkhtmltopdf exe
我正在尝试使用 PDFKit。 它看起来相当简单,所以我试图关注 Ryan's Rails cast . 所以我将 gem 添加到我的 GemFile 并更新了我的 application.rb和:
我想知道是否有人知道如何在运行 pdfkit 时实现超时? 我正在尝试遍历大量 URL 以打印每个 URL 页面的 pdf。有时循环可能会挂起,但是如果它花费超过 30 秒,我只想跳过循环中的 URL
是否可以使用 PDFKit 在矩形上呈现文本。也许可以使用hack,用不透明度填充矩形 - 但我不想那样使用它。我的文本被矩形隐藏(我通过交替使用不同颜色的矩形创建表格)。 更新 我发现文本在某种程度
在我的应用程序中,我使用 PDFKit 来显示简单的 PDF。 PDF 显示在 ViewController 上。 PDF 要设置为屏幕宽度,仅此而已。我在 Storyboard 中将 PDFView
我有一个使用 PDFKIT 创建的 PDF View ,它在 iPad 中加载 PDF。当我们缩放 PDF 并进行一些手绘时,它会崩溃。可能是什么问题? 最佳答案 我有一个类似的问题,放大具有多个子类
我正在使用 PDFKit在浏览器中生成 PDF,然后将该 PDF 的 blob 数据发送到新选项卡以供用户下载。我真的很喜欢 PDFKit...它非常漂亮。 但是,当我尝试使用 PDFDocument
我正在尝试整合 PSPDFKit对于我的项目中的 iOS,我还没有成功。下载Demo版后,将框架添加到我的项目中,添加所有需要的库并放置 #import 在“前缀”文件上,我收到链接器错误: ld:
我一直在网上搜索使用 pdfkit(python 包装器)实现页眉和页脚的示例,但找不到任何示例。 谁能展示一些如何使用 pdfkit python 包装器在 wkhtmltopdf 中实现选项的示例
我想在 pdf 中找到一个特定的单词并在其上绘制一个边界框。为此,我使用 func findString(_ string: String, withOptions options: NSString
我正在使用 Apple 的 PDFKit,我正在使用他们的 sample app for adding a watermark到pdf文档。 除我拥有的一份 pdf 文件外,该代码按预期工作。这个特定
是否可以使用 PDFkit 将文本中的“ěščřžýáé”等字符保存为 PDF? 他们的浏览器演示在这里: http://pdfkit.org/demo/browser.html 注意:我只使用js库
如何使用 PDFKit 在 PDFView 中禁用滚动反弹? 显示 PDF 的 View 没有滚动反弹选项。 这是我的代码: if let path = Bundle.main.path(forRes
我正在尝试在 iOS 上使用 PDFKit 向文档添加高亮注释。 let highlight = PDFAnnotation(bounds: selection.bounds(for: page),
是否可以在 PDFKit 中更改 FreeText 注释的文本(即 contents)而不删除注释/构建新注释? 在 PDFView 中查看时,以下代码片段不会更改注释的内容: let url = B
使用python pdfkit生成pdf文件中遇到中文乱码问题 1.生成的文件名不能带有中文字符 2.生成的pdf内容中文为乱码 生成的文件名不能带有中文字符 解决方法: 我暂时想到的处理
如何从 pt/px 设置 characterSpacing? 如何从 pt/px 设置 lineHeight? 我只是不明白如何将点或像素转换为 pdfkit 的值。请帮帮我 https://gith
我试图了解如何让 PDF Kit 在 React JS 中工作。 我的简单要求是使用 ReactJS 和 PDFKit 在浏览器中呈现文件。 查看教程,引用了一些 PDFKit 可以在浏览器中工作的选
我想控制pdf文件的缩放级别。默认情况下,当它启动时,它不适合屏幕。即使手动(捏缩放)放大和缩小也不能使其适合页面。我已将 autoScales 设置为 true。 还尝试使用以下变量,但没有帮助
我是一名优秀的程序员,十分优秀!