1

我正在使用此代码在整个应用程序中自定义导航栏图像。

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 图像,我怎样才能让它保持原来的外观?

我知道我可以使用appearanceWhenContainedIn:,但是如果我将 nil 作为图像返回,我只会得到一个黑色空间。

4

1 回答 1

2

我首先想到的是在自定义之前简单地从导航栏中获取默认图像。我很震惊,它起作用了。

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 上的高级外观自定义有一个巧妙的技巧,将外观代理转换为适当类的实例,因此编译器可以警告无法识别的选择器,以及代码完成更精确。

于 2012-09-15T22:00:00.053 回答