如何在特定的ViewController中手动更改导航栏背景图像?

时间:2010-11-18 17:06:07

标签: objective-c ios4 navigation

当我在特定的ViewController中时,我正在尝试更改导航栏的图像背景。我的app模型: - appDelegate      - tabbarcontroller         -viewcontroller1         -navigationBarController             -viewcontroller2             -viewcontroller3(*)         -viewcontroller4         -viewcontroller5

我已经在appDelegate中实现了这段代码:

static NSMutableDictionary *navigationBarImages = NULL;

    @implementation UINavigationBar(CustomImage)

    + (void)initImageDictionary
    {
        if(navigationBarImages==NULL){
            navigationBarImages=[[NSMutableDictionary alloc] init];
        }   
    }
    - (void)drawRect:(CGRect)rect
    {
        UIColor *color = [UIColor blackColor];
        NSString *imageName=[navigationBarImages objectForKey:[NSValue valueWithNonretainedObject: self]];
        if (imageName==nil) {
            imageName=@"navbar.png";
        }
        UIImage *image = [UIImage imageNamed:imageName];
        [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        self.tintColor = color;

    }

    - (void)setImage:(UIImage*)image
    {
        [navigationBarImages setObject:image forKey:[NSValue valueWithNonretainedObject: self]];
    }
    @end

之后,我在viewcontroller2中编写了这段代码:

[UINavigationBar initImageDictionary];

这是工作,我的导航栏就像我希望的那样,但是在我的viewcontroller3中,我写道:

UIImage *navBarLandscape = [UIImage imageNamed:@"navbar.png"];
    [[[self navigationController] navigationBar] setImage:navBarLandscape];

并且它没有做到这一点,我有一个错误,我无法承担:

[UIImage length]:无法识别的选择器发送到实例0x5a58130 2010-11-18 18:02:37.170 finalAudi [1463:307] * 由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [UIImage长度]:无法识别的选择器发送到实例0x5a58130'

你有什么想法吗?

关于马球。

3 个答案:

答案 0 :(得分:2)

在每个viewController的viewDidLoad尝试:

/*  make sure that we set the image to be centered  */ 
/*  you will ahve to make minor adjustments depending on the dimensions of your image.  */
UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake(self.navigationController.navigationBar.frame.size.width/2-75,0,150,
                                                       self.navigationController.navigationBar.frame.size.height-1)];
[logo setImage:[UIImage imageNamed:@"myImage.png"]];
[self.navigationController.navigationBar addSubview:logo];
[self.navigationController.navigationBar sendSubviewToBack:logo];

以及viewWillAppear:viewDidDisappear:尝试:

/*  This will ensure that if you have a different image for each view that the previous viewController's navBar image wont stay.  */
-(void)viewWillDisappear:(BOOL)animated {logo.hidden = YES;}
-(void)viewWillAppear:(BOOL)animated    {logo.hidden = NO ;}

并确保旋转时图像保持居中:

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    if(((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || 
    (interfaceOrientation == UIInterfaceOrientationLandscapeRight))){
        [logo setFrame:CGRectMake(self.navigationController.navigationBar.frame.size.width/2-75,0,150,self.navigationController.navigationBar.frame.size.height-1)];
    }else if(((interfaceOrientation == UIInterfaceOrientationPortrait) || 
          (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))){
        [logo setFrame:CGRectMake(self.navigationController.navigationBar.frame.size.width/2-80,0,150,self.navigationController.navigationBar.frame.size.height-1)];
    }
}

答案 1 :(得分:1)

感谢大家,我能够在appdelegate中解决我的问题:

@implementation UINavigationBar(CustomImage)

+ (void)initAppDelegate
{
    if (applicationDelegate==Nil) {
        applicationDelegate = (finalAudiAppDelegate *)[[UIApplication sharedApplication] delegate];
    }
}
- (void)drawRect:(CGRect)rect
{
    UIColor *color = [UIColor blackColor];
    [[[applicationDelegate backNavBar] objectAtIndex:[applicationDelegate indexBackNavBar]] drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    self.tintColor = color;
}
@end

我正在使用一个NSMutableArray来存储我的navigationBar的背景图像和一个NSInteger(indexBackNavBar),我可以通过UIApplication sharedApplication访问它们。 然后在我的viewcontroller中,我只在viewWillAppear中设置indexBackNavBar = 1,在viewWillDisappear中设置indexBackNavBar = 0。瞧!

答案 2 :(得分:0)

如果我理解正确,我会建议查看UINavigationBarDelegate并实现代码以在那里切换导航栏的背景,而不是将其传播到多个viewControllers。

委托具有在推送和弹出新视图之前将被调用的方法,因此从设计角度来看可能会更好