gpt4 book ai didi

ios - QLPreviewController 更改标题?

转载 作者:IT王子 更新时间:2023-10-29 05:41:25 31 4
gpt4 key购买 nike

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

我已经尝试过:

  1. 子类化 QLPreviewController
  2. 添加

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

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

有什么想法吗?

最佳答案

如果您需要显示与您的 url 中的 lastPathComponent 不同的标题,您可以将 QLPreviewItem 子类化并提供您自己的标题实现可选属性:

实例属性声明:

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.

第一个子类 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
}
}

然后在您的 Controller 中返回 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] }
}

关于ios - QLPreviewController 更改标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43055902/

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