如何在qlpreviewcontroller中自定义导航栏的颜色 [英] How to customize the color of the navigation bar in qlpreviewcontroller
问题描述
我可以在QlPreviewController控制器中自定义导航栏的颜色吗?
Can I customize the color of the navigation bar in the QlPreviewController controller?
我已经尝试关注
[[UINavigationBar appearanceWhenContainedIn: [QLPreviewController class], nil] setBarTintColor: [UIColor redColor]];
但它不起作用.
谢谢.
推荐答案
是的,如果您通过presentViewController: animated:
这是我的解决方案,请使用带有1x1图像的 setBackgroundImage:而不是 setBarTintColor:
Here's my solution, use setBackgroundImage: with 1x1 image instead of setBarTintColor:
[[UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[QLPreviewController class]]]
setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]]
forBarMetrics:UIBarMetricsDefault];
imageWithColor:是我的UIImage自定义类别中的一种方法,该方法返回可调整大小的1x1所需颜色的图像(在上面的示例中为红色):
And imageWithColor: is a method in my custom category of UIImage which is returning resizable 1x1 image of the desired color (red color in the example above):
+ (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版本检查将其包装起来,例如:
I also suggest to wrap this with iOS version check like:
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)
这篇关于如何在qlpreviewcontroller中自定义导航栏的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!