navigationbar – 如何在qlpreviewcontroller中自定义导航栏的颜色

前端之家收集整理的这篇文章主要介绍了navigationbar – 如何在qlpreviewcontroller中自定义导航栏的颜色前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我可以自定义QlPreviewController控制器中导航栏的颜色吗?

我试过跟随

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

但它不起作用.

谢谢.

解决方法

是的,如果你通过presentViewController显示它,那么在iOS 11的QLPreviewController上有一个barTintColor的错误:animated:

这是我的解决方案,使用setBackgroundImage:使用1×1图像而不是setBarTintColor:

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

和imageWithColor:是我的自定义类UIImage中的一个方法,它返回所需颜色的可调整大小的1×1图像(上例中的红色):

+ (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)

猜你在找的iOS相关文章