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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:51:55  来源:igfitidea点击:

UINavigationBar setBackgroundImage: forBarMetrics: Not Working

objective-cioscocoa-touchios5

提问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 imageto 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 nbisn'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

You didn't do something like use a UINavigationBar subclass? If you do this and override drawRect: on iOS5 and above, things will break.

你没有做类似使用 UINavigationBar 子类的事情吗?如果你这样做并覆盖 drawRect: 在 iOS5 及更高版本上,事情就会崩溃。

See my answer herefor a way to support both OS versions.

有关支持两个操作系统版本的方法,请在此处查看我的答案。

回答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;