如何在qlpreviewController中自定义导航栏的颜色



我可以在qlpreviewControllloll Controller中自定义导航栏的颜色?

我尝试关注

[[UINavigationBar appearanceWhenContainedIn: [QLPreviewController class], nil] setBarTintColor: [UIColor redColor]];

但行不通。

谢谢。

是的,iOS 11 在qlpreviewController上有一个错误,如果您是通过presentViewController: animated:

显示它

这是我的解决方案,使用 setbackgroundImage:带有1x1映像,而不是 setBartintColor:

[[UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[QLPreviewController class]]] 
    setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]]
 forBarMetrics:UIBarMetricsDefault];

imageWithColor:是我自定义类别中的一种方法,它正在返回所需颜色的可分解1x1图像(上面的示例中的红色):

+ (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    const CGFloat alpha = CGColorGetAlpha(color.CGColor);
    const BOOL opaque = alpha == 1;
    UIGraphicsBeginImageContextWithOptions(rect.size, opaque, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

我还建议用iOS版本检查以下检查:

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"11.0")) {
[[UINavigationBar appearance... 
setBackgroundImage:[UIImage imageWithColor:...]
     forBarMetrics:UIBarMetricsDefault];
    }

SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO来自:

#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

感谢亚历山大·拉什维奇(Aleksander Lashevich)的回答!奇迹般有效。为了方便起见,我将其转换为 Swift 4

let navbar = UINavigationBar.appearance(whenContainedInInstancesOf: [QLPreviewController.self])
navbar.setBackgroundImage(self.imageWithColor(color: UIColor.red), for: UIBarMetrics.default)

和图像生成:

func imageWithColor(color: UIColor) -> UIImage {
    let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
    let alpha = color.cgColor.alpha
    let opaque = alpha == 1
    UIGraphicsBeginImageContextWithOptions(rect.size, opaque, 0)
    let context = UIGraphicsGetCurrentContext()
    context?.setFillColor(color.cgColor)
    context?.fill(rect)
    return UIGraphicsGetImageFromCurrentImageContext()!
}

如果您不想出于何种原因使用外观代理,则如果您子类QLPREVIEWCONTROLLER,则需要另一种方法来解决此问题。事实证明,如果您将QLPReviewController表示态度,则会创建一个UinavigationController,然后将其作为子viewController添加到视图层次结构中。但是,这不会在初始化或甚至在调用查看迪德荷之前发生。当即将介绍QLPReviewController时,它会发生。因此,覆盖 ViewWillAppear function 在qlpreviewController subclass 中是解决此处的方式:

public override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if let nc = self.children.first as? UINavigationController {
        // your customization code goes here
        nc.navigationBar.tintColor = .yellow
    }
}

我使用此解决方法,因为所有其他方法都不适合我。

import QuickLook
class PreviewController: QLPreviewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.navigationBar.setBackgroundImage(nil, for: .any, barMetrics: .default)
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.navigationBar.isTranslucent = false //optional
    }
}

请在应用程序委托中使用以下代码

[[uinavigationbar外观] setbartintColor:#your color#];

相关内容

  • 没有找到相关文章

最新更新