QLPreviewController NavigationBar barTintColor

时间:2017-12-06 11:49:14

标签: ios uiviewcontroller uinavigationcontroller swift4 qlpreviewcontroller

当我self.present() QLPreviewController时,其NavigationBar会失去颜色,这是在AppDelegate中实现的。

    UINavigationBar.appearance().isTranslucent = false
    UINavigationBar.appearance().tintColor = UIColor.white
    UINavigationBar.appearance().barTintColor = Colors.fifth   // Blue color
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    UINavigationBar.appearance().shadowImage = UIImage()

QLPreviewController实施:

let preview = QLPreviewController()
preview.dataSource = self

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

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

fileprivate func showDocument(fileId: Int) {
    SVProgressHUD.show()
    self.fileService.download(id: fileId) {
        url, error in

        if error == nil {
            self.fileURL = url
            if QLPreviewController.canPreview(self.fileURL! as QLPreviewItem) {
                self.present(self.preview, animated: true, completion: nil)
                SVProgressHUD.dismiss()
            } else {
                SVProgressHUD.showDismissableError(with: "Произошла ошибка во время чтения файла \(url!.lastPathComponent)")
            }
        } else {
            SVProgressHUD.showDismissableError(with: error?.localizedDescription)
        }
    }
}

我尝试self.show(),其NavigationBar 确定! 我想将QLPreviewController's NavigationBar颜色设置为与AppDelegate's UINavigationBar具有相同的设置。如何以编程方式更改此设置?提前谢谢!

1 个答案:

答案 0 :(得分:0)

首先将 UINavigationController QLPreviewController 实例化为根视图控制器。然后,您可以显示该导航栏,该导航栏将具有您在app delegate class中设置的属性。

    let preview = QLPreviewController()
    preview.dataSource = self
    preview.currentPreviewItemIndex = 0
    let navBar = UINavigationController(rootViewController: preview)
    let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(self.dismissQLPreviewController))
    preview.navigationItem.leftBarButtonItem = doneButton
    self.present(navBar, animated: true)