如何在quicklook中更改导航栏颜色和 Headers ?

我使用下面的代码来更改导航栏颜色

import UIKit import QuickLook类ViewController:UIViewController,QLPreviewControllerDataSource {

let itemURL = URL(string: "https://www.ets.org/Media/Tests/GRE/pdf/gre_research_validity_data.pdf")!
var fileURL = URL(string: "")

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

    let quickLookController = QLPreviewController()
    quickLookController.dataSource = self


    UINavigationBar.appearance().barTintColor = UIColor.clear
    UINavigationBar.appearance(whenContainedInInstancesOf: [QLPreviewController.self]).backgroundColor = UIColor.red
    // Download the pdf and get it as nsdata
    let data = NSData(contentsOf: itemURL)
    do {
        // Get the documents directory
        let documentsDirectoryURL = try! FileManager().url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
        // Give the file a name and append it to the file path
        fileURL = documentsDirectoryURL.appendingPathComponent("sample.pdf")
        // Write the pdf to disk
        try data?.write(to: fileURL!, options: .atomic)

        // Make sure the file can be opened and then present the pdf
        if QLPreviewController.canPreview(itemURL as QLPreviewItem) {
            quickLookController.currentPreviewItemIndex = 0
            present(quickLookController, animated: true, completion: nil)
        }
    } catch {
        // cant find the url resource
    }
}

func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
    return 1
}

func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
    return fileURL! as QLPreviewItem
}

}

但我得到的是下面的图像,我需要红色的导航栏 .

enter image description here

回答(1)

2 years ago

请尝试以下代码:

self.navigationBar.barTintColor = UIColor.red
self.navigationBar.isTranslucent = false