gpt4 book ai didi

swift 4 : Change/Clear PDFView content

转载 作者:行者123 更新时间:2023-11-28 14:01:58 37 4
gpt4 key购买 nike

我想在 Swift 4 中使用 PDFView 框架。( https://developer.apple.com/documentation/pdfkit/pdfview )

以下函数接收 PDF 文档的路径。如果路径有效,则 PDF 文件成功显示。当我调用 openMe(path: String) 两次时出现问题。在这种情况下,旧内容仍然存在,并添加了新内容。我只是想用新内容更改旧内容。

private var pdfData: NSData? = nil

func openMe(path: String) {
let fileManager = FileManager.default

if fileManager.fileExists(atPath: path){
let url = NSURL.fileURL(withPath: path)
pdfData = NSData(contentsOfFile: path)
let pdfView = PDFView(frame: self.view.frame)
pdfViewController?.pdfViewControllerInformsMeasurementDataViewController = self
pdfView.document = PDFDocument(url: url)
pdfView.autoScales = true
pdfView.maxScaleFactor = 0.5
pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

self.pdfViewController?.view.addSubview(pdfView)
self.show(self.pdfViewController!, sender: nil)
}
}

编辑

引用excitedmicrobe的回答:我只是更改了答案中显示的代码,但导航 Controller 和 PDFView 之间的距离不同。

拳头 openMe 调用:

enter image description here

第二次 openMe 调用:

enter image description here

最佳答案

在这种情况下,您需要将 pdfView 设为全局:

viewDidLoad()之前添加如下内容:

var pdfView = PDFView()

override func viewDidLoad() {
super.viewDidLoad()

// ....
}

然后将您的代码 openMe() 编辑为:

func openMe(path: String) {
if self.pdfViewController?.view.subviews.contains(pdfView) {
self.pdfView.removeFromSuperview() // Remove it
} else {
// Do Nothing
}

let fileManager = FileManager.default

if fileManager.fileExists(atPath: path){
let url = NSURL.fileURL(withPath: path)
pdfData = NSData(contentsOfFile: path)
self.pdfView = PDFView(frame: self.view.frame)
pdfViewController?.pdfViewControllerInformsMeasurementDataViewController = self
pdfView.document = PDFDocument(url: url)
pdfView.autoScales = true
pdfView.maxScaleFactor = 0.5
pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

self.pdfViewController?.view.addSubview(pdfView)
self.show(self.pdfViewController!, sender: nil)
}
}

关于 swift 4 : Change/Clear PDFView content,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53477361/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com