开发者

Remove background image from navigationBar when pushing TableViewControllers

I have a navigation based app where I push TableViewControllers onto the stack. I would like to add a background image to the first TableViewControllers. Not a color, but an image.

I have added a categroy for setting and removing the background image.

@implementation UINavigationBar (UINavigationBarCategory) 

-(void)setBackgroundImage:(UIImage*)image withTag:(NSInteger)bgTag {
    if(image == NULL){ //might be called with NULL argument
        return;
    }
    UIImageView *aTabBarBackground = [[UIImageView alloc]initWithImage:image];
    aTabBarBackground.frame = CGRectMake(self.frame.size.width/2-51,0,102,44);
    aTabBarBackground.tag = bgTag;
    [self addSubview:aTabBarBackground];
    [self sendSubviewToBack:aTabBarBackground];
    [aTabBarBackground release];
}

- (void) clearBackgroundImage {
    NSArray *subviews = [self subviews];
    for (int i = [subviews count] - 1; i >= 0; i--) {
        if ([[subviews objectAtIndex:i] isMemberOfClass:[UIImageView class]]) {
            [[subviews objectAtIndex:i] removeFromSuperview];
        }
    }    
}

And am setting the image with the following code:

[[navigationController navigationBar] setBackgroundImage:[UIImage imageNamed:@"AppTop-Banner.png"]];

The background image is being added properly but for the subsequent pushed into view Tableviewcontrollers, I can't remove the image by calling the following:

@implementation SecondTableViewController

-(id) init {
    self = [super init];
    if (self != ni开发者_如何学Cl) {
        self.title = @"Categories";
    }
}

- (void)viewDidAppear:(BOOL)animated { 
        [super viewDidAppear:animated];  
        [self.navigationController.navigationBar clearBackgroundImage];
}  

In this case "clearBackgroundImage" is never being called and the background image is still there, with the title overlapping the image. I am not sure if I can refer to the navigationBar this way. Maybe self.navigationController.navigationBar is not the right way to call the relevant navigationBar Can somebody throw some light please.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜