我想在iphone中移动到另一个视图时更改导航栏的背景图像?

时间:2012-01-31 08:53:43

标签: iphone ios5 uinavigationbar

我正在开发一个应用程序,我必须在导航栏上放置图像,每当用户移动到另一个视图时,不同的图像必须出现在导航栏中。有人可以给我一些指导如何做到这一点吗?

2 个答案:

答案 0 :(得分:1)

这可能更像是一种通用方法。

为UINavigationBar和“.h”文件

创建一个类别
@interface  UINavigationBar (UINavigationBarCategory)

-(void)setBackgroundImage:(UIImage*)image;

@end

和“.m”文件

@implementation UINavigationBar (UINavigationBarCategory)

-(void)setBackgroundImage: (UIImage*)image
{

    if (image)
    {

        UIImageView *img = [[UIImageView alloc]initWithImage: image];
        aTabBarBackground.frame = CGRectMake(60,0, 200, 45);//Frame depends upon you 
        [self addSubview:img];
        [img release];
    }

}
@end

然后从每个Controller的'viewwillAppear'中你可以做到这样的事情:

 [self.navigationController.navigationBar setBackgroundImage: @"abc.png"];

答案 1 :(得分:0)

试试这段代码......

UINavigationBar *navigationBar=[[self navigationController]navigationBar];
UIImage *backgroundImg =[UIImage imageNamed:@"example.png"];
[navigationBar setBackgroundImage:backgroundImg forBarMetrics:UIBarMetricsDefault];

viewwillappear 中添加此代码,并提供要显示的图片名称,而不是example.png