我可以在QlPreviewController控制器中自定义导航栏的颜色吗?
我试过以下
ping redishost2
但它不起作用。
感谢。
答案 0 :(得分:11)
是的,如果您通过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)
答案 1 :(得分:2)
感谢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()!
}
答案 2 :(得分:1)
如果你们出于某种原因不想使用外观代理,那么如果将QLPreviewController子类化,还有另一种方法可以解决此问题。事实证明,如果以模态形式呈现QLPreviewController,它将创建一个UINavigationController,然后将其作为子ViewController添加到视图层次结构中。但是,这在初始化时甚至在调用viewDidLoad之前都不会发生。当即将出现QLPreviewController时,就会发生这种情况。因此,在QLPreviewController子类中覆盖 viewWillAppear 函数是这里的方法:
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
}
}
答案 3 :(得分:0)
我使用此替代方法是因为其他所有人都不为我工作。
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
}
}
答案 4 :(得分:-1)
请在App代理中使用以下代码
[[UINavigationBar appearance] setBarTintColor:#your color#];