推送 TableViewControllers 时从导航栏中删除背景图像

发布于 2024-10-28 12:37:44 字数 1689 浏览 3 评论 0原文

我有一个基于导航的应用程序,我将 TableViewControllers 推入堆栈。我想向第一个 TableViewController 添加背景图像。不是颜色,而是图像。

我添加了一个用于设置和删除背景图像的类别。

@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];
        }
    }    
}

我使用以下代码设置图像:

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

背景图像已正确添加,但对于随后推入视图的 Tableviewcontrollers,我无法通过调用以下命令来删除图像:

@implementation SecondTableViewController

-(id) init {
    self = [super init];
    if (self != nil) {
        self.title = @"Categories";
    }
}

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

在这种情况下,“clearBackgroundImage”永远不会被调用,并且背景图像仍然存在,标题与图像重叠。我不确定是否可以通过这种方式引用导航栏。也许 self.navigationController.navigationBar 不是调用相关 navigationBar 的正确方法,有人可以提供一些说明吗?

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 != nil) {
        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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文