小编典典

QLPreviewController更改标题吗?

swift

是否可以在QLPreviewController中更改项目的标题?

我已经尝试过:

  1. 子类化QLPreviewController
  2. override func viewDidAppear(_ animated: Bool) {
    

    self.navigationController?.navigationBar.topItem?.title = “Bericht”
    }

但是您只会在1/4秒内看到标题。

有任何想法吗?


阅读 531

收藏
2020-07-07

共1个答案

小编典典

如果您需要从URL显示除lastPathComponent以外的其他标题,则可以子类QLPreviewItem并提供实现可选属性的自己的标题:

实例属性声明:

var previewItemTitle: String? { get }

要显示的预览项目的标题。

如果您没有为此属性实现getter方法,或者您的方法返回nil,则QuickLook会检查要预览的项目的URL或内容,以确定要显示给用户的适当标题。返回此属性的非null值以提供自定义标题。


 protocol QLPreviewItem : NSObjectProtocol

描述

QLPreviewItem协议定义了您实现的属性,以使应用程序的内容在QuickLook预览(iOS中的QLPreviewController或macOS中的QLPreviewPanel)中可见。该协议中的方法也被声明为NSURL类的类别。因此,您可以将NSURL对象直接用作预览项目-
前提是您要使用这些项目的默认标题。默认标题是项目URL的最后一个路径部分。如果要提供自己的预览项目标题,请创建采用此协议的自己的预览项目对象。

第一子类QLPreviewItem:

import UIKit
import QuickLook
class PreviewItem: NSObject, QLPreviewItem {
    var previewItemURL: URL?
    var previewItemTitle: String?
    init(url: URL? = nil, title: String? = nil) {
        previewItemURL = url
        previewItemTitle = title
    }
}

然后在您的控制器中返回QLPreviewItem而不是URL:

Xcode 11•Swift 5.1

import UIKit
import QuickLook

class ViewController: UIViewController, QLPreviewControllerDelegate, QLPreviewControllerDataSource {

    var previewItems: [PreviewItem] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        previewItems = [
            .init(url: Bundle.main.url(forResource: "your file 1", withExtension: "ext"),
                  title: "Custom Title 1"),
            .init(url: Bundle.main.url(forResource: "your file 2", withExtension: "ext"),
                  title: "Custom Title 2"),
        ]
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        quickLook()
    }

    func numberOfPreviewItems(in controller: QLPreviewController) -> Int { previewItems.count }

    func quickLook(at index: Int = 0) {
        let controller = QLPreviewController()
        controller.delegate = self
        controller.dataSource = self
        controller.currentPreviewItemIndex = index
        present(controller, animated: true)
    }

    func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem { previewItems[index] }
}
2020-07-07