在QLPreviewController中更改导航栏颜色

时间:2018-02-16 07:22:49

标签: ios iphone xcode swift3 ios11

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

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

3 个答案:

答案 0 :(得分:3)

我使用下面的代码更改了swift3.0中QLPreviewController的导航栏颜色

UINavigationBar.appearance().barTintColor = UIColor.red

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

答案 1 :(得分:1)

在呈现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()
    }

答案 2 :(得分:-1)

将以下代码放在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)
相关问题