草庐IT

objective-c - UINavigationBar setBackgroundImage : forBarMetrics: Not Working

coder 2023-07-27 原文

我刚刚切换到 iOS 5,除了自定义导航栏外,我的应用程序中的一切似乎都正常工作。我环顾四周,听从了每个人关于调用新方法 setBackgroundImage: forBarMetrics: 的建议,但它似乎不起作用。这是我试图同时放在应用程序委托(delegate)和某些 View Controller 的 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];

不幸的是,这不起作用。如果有人有任何建议,我会洗耳恭听!

最佳答案

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

[[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 没有连接到任何东西。

关于objective-c - UINavigationBar setBackgroundImage : forBarMetrics: Not Working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7760819/

有关objective-c - UINavigationBar setBackgroundImage : forBarMetrics: Not Working的更多相关文章

随机推荐