问题描述
我正在使用此代码在我的应用中自定义导航栏图像。
I am using this code to customise my nav bar image throughout my app.
UIImage *navBarTexture = [[UIImage imageNamed:@"NavBarTexture_iPad"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:navBarTexture forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:navBarTexture forBarMetrics:UIBarMetricsLandscapePhone];
这可以创造奇迹。但是,在UIPopoverControllers的导航栏上使用此图像时,看起来有点奇怪。它的默认Apple图像是我想要使用的,如何保持原始外观?
This works wonders. However, when using this image on the nav bar in UIPopoverControllers, it looks a bit strange. The default Apple image for it is what I want to use, how can I make it retain that original appearance?
我知道我可以使用 appearanceWhenContainedIn :
但是如果我将nil作为图像返回,我只会得到一个黑色空间。
I know I can use appearanceWhenContainedIn:
however if I return nil as an image, I just get a black space.
非常感谢,谢谢。
推荐答案
第一件事我想到的只是在自定义之前从导航栏中获取默认图像。我很震惊它的确有效。
The first thing that crossed my mind is to simply get the default image from the navigation bar before customization. I was shocked that it worked.
UINavigationBar *appearanceProxBar = [UINavigationBar appearance];
UIImage *defaultImage = [appearanceProxBar backgroundImageForBarMetrics:UIBarMetricsDefault];
[appearanceProxBar setBackgroundImage:navBarTexture forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearanceWhenContainedIn:[UIPopoverController class], nil] setBackgroundImage:defaultImage forBarMetrics:UIBarMetricsDefault];
此外,正如您在答案中所看到的,WWDC 2012 - 216 - iOS上的高级外观自定义有一个巧妙的技巧,将外观代理转换为适当类的实例,因此编译器可以警告无法识别的选择器,并且代码完成也更精确。
Also, as you can see in the answer, the WWDC 2012 - 216 - Advanced Appearance Customization on iOS had a neat trick of casting the appearance proxy to an instance of the appropriate class so the compiler can warn of unrecognized selectors, and also code completion is more precise.
这篇关于UIPopoverController导航栏外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!