Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

ios - QLPreviewController change title?

Is it possible to change the title of an item in an QLPreviewController?

I've already tried with:

  1. Subclassing QLPreviewController
  2. Add

    override func viewDidAppear(_ animated: Bool) {
      self.navigationController?.navigationBar.topItem?.title = "Bericht"
    }
    

But you see the title only for maybe 1/4 second.

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you need to display a different title other than the lastPathComponent from your url, you can subclass QLPreviewItem and provide your own title implementing the optional property:

Instance Property Declaration:

var previewItemTitle: String? { get }

The title to display for the preview item.

If you do not implement a getter method for this property, or if your method returns nil, QuickLook examines the URL or content of the item being previewed to determine an appropriate title for display to the user. Return a non-nil value for this property to provide a custom title.


 protocol QLPreviewItem : NSObjectProtocol

Description

The QLPreviewItem protocol defines properties you implement to make your application’s content visible in a QuickLook preview (QLPreviewController in iOS or QLPreviewPanel in macOS). The methods in this protocol are also declared as a category on the NSURL class. As a result, you can use NSURL objects directly as preview items—provided that you want to use the default titles of those items. A default title is the last path component of an item’s URL. If you want to supply your own preview item titles, create your own preview item objects that adopt this protocol.

First Subclass 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
    }
}

Then in your controller you return the QLPreviewItem instead of the 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] }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...