ios UINavigationBar setBackgroundImage: forBarMetrics: 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7760819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UINavigationBar setBackgroundImage: forBarMetrics: Not Working
提问by Nick ONeill
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:
我刚刚切换到 iOS 5,除了自定义导航栏之外,我的应用程序中的所有内容似乎都在运行。我环顾四周并按照每个人的建议调用新方法 setBackgroundImage: forBarMetrics: 但是它似乎不起作用。这是我尝试放置在应用程序委托和某些视图控制器的 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];
Unfortunately this doesn't work. If anybody has any suggestions at all, I'm all ears!
不幸的是,这不起作用。如果有人有任何建议,我会全神贯注!
回答by Rob Bajorek
To apply image
to all your navigation bars, use the appearance proxy:
要应用于image
所有导航栏,请使用外观代理:
[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
For an individual bar:
对于单个酒吧:
// Assuming "self" is a view controller pushed on to a UINavigationController stack
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
In your example, the background image won't change because nb
isn't hooked up to anything.
在您的示例中,背景图像不会改变,因为nb
没有连接到任何东西。
回答by Kamleshwar
Answer by rob is correct but if application runs on iOS 4.3 or lower app will crash. So you can implement this like
rob 的回答是正确的,但如果应用程序在 iOS 4.3 或更低版本上运行,应用程序将崩溃。所以你可以像这样实现
if([[UINavigationBar class] respondsToSelector:@selector(appearance)]) //iOS >=5.0
{
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationBar.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navBar-Landscape.png"] forBarMetrics:UIBarMetricsLandscapePhone];
}
This will set image for both mode Landscape and Portrait
这将为模式横向和纵向设置图像
回答by RPeck
You need to get the navigation bar from a navigation controller. Right now you are just creating one and then it deallocs when you release it. You need to get the navigation controller for your view controller.
您需要从导航控制器获取导航栏。现在你只是创建一个,然后当你释放它时它会释放。您需要为您的视图控制器获取导航控制器。
UINavigationController * navigationController = [self navigationController];
UINavigationBar * nb = [navigationController navigationBar];
回答by DenVog
Note that if you want to apply the same to a toolbar, it is slightly different. Here's an example, building on Rob's answer:
请注意,如果要将相同的内容应用于工具栏,则略有不同。这是一个示例,基于 Rob 的回答:
[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:@"NavBarDarkWoodGrain.png"] forToolbarPosition:UIToolbarPositionBottom barMetrics:UIBarMetricsDefault];
回答by Paul de Lange
回答by Sunil Adhyaru
This worked for me.
这对我有用。
UIView *parentViewLeft = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
parentViewLeft.backgroundColor = [UIColor clearColor];
if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar.png"]
forBarMetrics:UIBarMetricsDefault];
} else {
UIImageView* imgBG = [[UIImageView alloc]init];
imgBG.image = [UIImage imageNamed:@"navbar.png"];
imgBG.frame = CGRectMake(-10,0, 330, 44);
[parentViewLeft addSubview:imgBG];
}
UIBarButtonItem *customBarButtomLeft = [[UIBarButtonItem alloc] initWithCustomView:parentViewLeft];
self.navigationItem.leftBarButtonItem = customBarButtomLeft;