3

I want to change navigation bar color of QLPreviewController in swift 3. I have used below code to change color but its not working

viewQLPreview = QLPreviewController()
viewQLPreview.dataSource = self
viewQLPreview.delegate = self
viewQLPreview.navigationController?.navigationBar.isTranslucent = false
viewQLPreview.navigationController?.navigationBar.tintColor = UIColor.red
Rushabh
  • 3,208
  • 5
  • 28
  • 51

3 Answers3

6

I have used below code to Change navigation bar color of QLPreviewController in swift 3.0

UINavigationBar.appearance().barTintColor = UIColor.red

UINavigationBar.appearance(whenContainedInInstancesOf: [QLPreviewController.self]).backgroundColor = UIColor.red
Rushabh
  • 3,208
  • 5
  • 28
  • 51
  • QLPreviewController is not found when I do this. UIKit is imported in the file. Any idea what to import in order to use QLPreviewController? – Kevin Nov 16 '21 at 09:11
  • QLPreviewController lives in QuickLook: `import QuickLook` – schmidiii Dec 22 '21 at 13:29
4

Use below code before present 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()
    }
-4

Put below code in viewDidLoad of QLPreviewController.

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

Also make sure that viewQLPreview.navigationController != nil

If you are pushing QLPreviewController then this code will work....

If you are presenting QLPreviewController then you need to make sure that rootController should be navigation controller, in your case..

let viewQLPreview = QLPreviewController()
let nav = UINavigationController(rootViewController: viewQLPreview)
self.present(nav, animated: true, completion: nil)
Mahendra
  • 8,448
  • 3
  • 33
  • 56