2

I am trying to use a background image using the appearance methods in iOS 5. I’m setting the background images with these statements:

UIImage *portraitImage = [UIImage imageNamed:@"test_bar_portrait.png"];
UIImage *landscapeImage = [UIImage imageNamed:@"test_bar_landscape.png"];
[[UINavigationBar appearance] setBackgroundImage:portraitImage forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:landscapeImage forBarMetrics:UIBarMetricsLandscapePhone];

When I run the app, the correct image appears in portrait and landscape, but when I rotate back to portrait, it still has the landscape image shown.

I created a sample test application here: http://dl.dropbox.com/u/7834263/Appearance%20Test.zip

And screen shots of what is happening:

normal portrait

normal landscape

incorrect portrait

4

2 回答 2

6

我尝试将您的代码更改为以下代码,它对我来说效果很好。

UIImage *portraitImage = [UIImage imageNamed:@"test_bar_portrait.png"];
UIImage *landscapeImage = [UIImage imageNamed:@"test_bar_landscape.png"];
[[UINavigationBar appearance] setBackgroundImage:portraitImage forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:landscapeImage forBarMetrics:UIBarMetricsLandscapePhone];

self.navigationController.navigationBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;

请检查一下。

于 2012-04-30T13:33:24.830 回答
0

斯威夫特 2.0:

let portraitImage: UIImage = UIImage(named: "Navbar.portrait.png")!
let landscapeImage: UIImage = UIImage(named: "Navbar.landscape.png")!
UINavigationBar.appearance().setBackgroundImage(portraitImage, forBarMetrics: .Default)
UINavigationBar.appearance().setBackgroundImage(landscapeImage, forBarMetrics: .CompactPrompt)
self.navigationController!.navigationBar.autoresizingMask = .FlexibleTopMargin
于 2016-02-11T08:10:25.120 回答