UINavigationBar setBackgroundImage:forBarMetrics:Not Working

Nic*_*ill 13 cocoa-touch objective-c ios ios5

我刚刚切换到iOS 5,除了自定义导航栏之外,一切似乎都在我的应用程序中工作.我环顾四周,并按照每个人的建议调用新方法setBackgroundImage:forBarMetrics:但它似乎不起作用.这是我试图将它们放在app委托中以及某些视图控制器的viewDidLoad方法中的代码:

UINavigationBar *nb = [[UINavigationBar alloc]init];
if( [nb respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] )
{
    UIImage *image = [UIImage imageNamed:@"navBarBackground.png"];
    [nb setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
[nb release];
Run Code Online (Sandbox Code Playgroud)

不幸的是,这不起作用.如果有人有任何建议,我全都耳朵!

Rob*_*rek 44

要应用于image所有导航栏,请使用外观代理:

[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)

对于个人酒吧:

// Assuming "self" is a view controller pushed on to a UINavigationController stack
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)

在您的示例中,背景图像不会更改,因为nb它没有连接到任何内容.

  • 在iOS 7上,这似乎做了一些非常奇怪的平铺 (8认同)