6

我可以自定义QlPreviewController控制器中导航栏的颜色吗?如何自定义qlpreviewcontroller中导航栏的颜色

我曾尝试以下

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

,但它不工作。

谢谢。

+0

请不要标记如下所示的代码:\'code \',而是在代码前添加四个空格。也请解释什么是'不工作'。运行此类代码时,您的系统是否会爆炸? – SteveFest

回答

-1

请应用委托使用以下代码

[[UINavigationBar的外观] setBarTintColor:#your颜色#]。

+0

*在AppDelegate.m –

5

是啊,有一个与barTintColor上QLPreviewController为iOS 一个错误,如果你是显示它通过presentViewController: animated:

这里是我的解决方案,使用了setBackgroundImage:使用1x1图片代替setBarTintColor:

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

imageWithColor:是我的自定义类别的UIImage中的方法,它返回可调整大小的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) 
+0

不错的建议,通过设置图像为我工作。 – johnrechd