QLPreviewController change header?

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

I already tried:

  • Subclass QLPreviewController
  • Add

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

But you only see the name, perhaps 1/4 of a second.

Any ideas?

+5
source share
1 answer

If you need to display a different header other than lastPathComponent from your URL, you can subclass QLPreviewItem and provide your own custom header that implements an optional property:

Instance Property Declaration:

 var previewItemTitle: String? { get } 

The title to display for the preview item.

If you do not use the getter method for this property, or if your method returns nil, QuickLook checks the URL or content of the element to preview the corresponding header for the user to display. Returns the non-nil value for this property to provide a custom header.


  protocol QLPreviewItem : NSObjectProtocol 

Description

The QLPreviewItem protocol defines the properties that you implement to make your application content visible in the QuickLook preview (QLPreviewController on iOS or QLPreviewPanel on macOS). Methods in this protocol are also declared a category of the NSURL class. As a result, you can directly use NSURL objects as a preview, provided that you want to use the default headers for these elements. The default header is the last component of the path for the URL of the elements. if you want to provide your own preview headers, create your own preview of objects that accept this protocol.

First subclass of QLPreviewItem:

 import UIKit import QuickLook class PreviewItem: NSObject, QLPreviewItem { var previewItemURL: URL? var previewItemTitle: String? } 

Then in your controller, you return QLPreviewItem instead of the URL:

 import UIKit import QuickLook class ViewController: UIViewController, QLPreviewControllerDelegate, QLPreviewControllerDataSource { let item = PreviewItem() override func viewDidLoad() { super.viewDidLoad() item.previewItemTitle = "Custom Title" item.previewItemURL = Bundle.main.url(forResource: "your file", withExtension: "ext") } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) quickLook() } func numberOfPreviewItems(in controller: QLPreviewController) -> Int { return 1 } func quickLook() { let preview = QLPreviewController() preview.delegate = self preview.dataSource = self preview.currentPreviewItemIndex = 0 present(preview, animated: true) } func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem { return item } } 
+5
source

Source: https://habr.com/ru/post/1265976/


All Articles