在 QLPreviewController 中更改导航栏颜色

Change navigation bar color in QLPreviewController

我想在 swift 中更改 QLPreviewController 的导航栏颜色 3. 我使用下面的代码更改颜色但它不起作用

viewQLPreview = QLPreviewController()
viewQLPreview.dataSource = self
viewQLPreview.delegate = self
viewQLPreview.navigationController?.navigationBar.isTranslucent = false
viewQLPreview.navigationController?.navigationBar.tintColor = UIColor.red

将下面的代码放在 QLPreviewController 的 viewDidLoad 中。

viewQLPreview.navigationController?.navigationBar.isTranslucent = false
viewQLPreview.navigationController?.navigationBar.tintColor = UIColor.red

还要确保 viewQLPreview.navigationController != nil

如果您正在推送 QLPreviewController 那么此代码将有效....

如果您要展示 QLPreviewController 那么您需要确保 rootController 应该是导航控制器,在您的情况下..

let viewQLPreview = QLPreviewController()
let nav = UINavigationController(rootViewController: viewQLPreview)
self.present(nav, animated: true, completion: nil)

在当前 QLPreviewController 之前使用以下代码:

UINavigationBar.appearance().tintColor = UIColor.red
UINavigationBar.appearance().barTintColor = UIColor.blue
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.red]
UINavigationBar.appearance().setBackgroundImage(fromColor(color: UIColor.blue), for: .default)
UINavigationBar.appearance().isTranslucent = false 

func fromColor (color: UIColor) -> UIImage{
        let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContext(rect.size)
        let context: CGContext? = UIGraphicsGetCurrentContext()
        context?.setFillColor(color.cgColor)
        context?.fill(rect)
        let image: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image ?? UIImage()
    }

我使用下面的代码在 swift 3.0

中更改 QLPreviewController 的导航栏颜色
UINavigationBar.appearance().barTintColor = UIColor.red

UINavigationBar.appearance(whenContainedInInstancesOf: [QLPreviewController.self]).backgroundColor = UIColor.red