UINavigationBar setBackgroundImage:forBarMetrics:Not Working [英] UINavigationBar setBackgroundImage: forBarMetrics: Not Working
问题描述
我只是切换到iOS 5,除了自定义导航栏,一切似乎都在我的应用程序工作。我环顾四周,并遵循每个人的建议,调用新的方法setBackgroundImage:forBarMetrics:但它似乎不工作。这是我试图放置在应用程序委托和一些视图控制器的viewDidLoad方法内的代码:
I just switched over to iOS 5 and everything appears to be working in my application aside from the custom navigation bar. I looked around and followed everybody's suggestion of calling the new methods setBackgroundImage: forBarMetrics: however it doesn't appear to work. This is the code I've tried to place both within the app delegate and within the viewDidLoad method of some of the view controllers:
UINavigationBar *nb = [[UINavigationBar alloc]init];
if( [nb respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] )
{
UIImage *image = [UIImage imageNamed:@"navBarBackground.png"];
[nb setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
[nb release];
不幸的是这不行。
推荐答案
要应用 image code>到所有导航栏,使用外观代理:
To apply image
to all your navigation bars, use the appearance proxy:
[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
个别酒吧:
// Assuming "self" is a view controller pushed on to a UINavigationController stack
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
在您的示例中,背景图片将不会更改,因为 nb
没有连接到任何东西。
In your example, the background image won't change because nb
isn't hooked up to anything.
这篇关于UINavigationBar setBackgroundImage:forBarMetrics:Not Working的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!