设置导航栏背景图片后自定义后退按钮消失

发布于 2024-11-26 22:26:45 字数 1624 浏览 1 评论 0原文

我正在使用以下类别代码来更改 导航栏的背景图像

#import <Foundation/Foundation.h>

@interface UINavigationBar (CustomImage)
- (void) setBackgroundImage:(UIImage*)image;
- (void) clearBackgroundImage;
@end


#import "UINavigationBar+CustomImage.h"

@implementation UINavigationBar (CustomImage)
- (void) setBackgroundImage:(UIImage*)image {
    if (image == NULL) return;
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    [self insertSubview:imageView atIndex:0];
    [imageView release];
}

- (void) clearBackgroundImage {    
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSArray *mySubviews = [self subviews];

    for (int i = [mySubviews count] - 1; i >= 0; i--)
    {
        if ([[mySubviews objectAtIndex:i] isMemberOfClass:[UIImageView class]])
        {
            [[mySubviews objectAtIndex:i] removeFromSuperview];
            return;
        }
    }

    [pool release];
}
@end

我使用以下代码生成自定义后退按钮 在我看来

UIButton *btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
[btnBack addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
[btnBack setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
UIBarButtonItem *barBtnBack = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
self.navigationItem.leftBarButtonItem = barBtnBack;

[btnBack release];
[barBtnBack release];

,但按钮大多数时候都隐藏在背景图像下 有时它会随机出现。 为什么会发生这种情况?我不确定图像插入索引 0 处有什么问题 据我了解,它应该一直落后。 请帮忙。

I'm using the following categories code to change
the background image of the navigation bar

#import <Foundation/Foundation.h>

@interface UINavigationBar (CustomImage)
- (void) setBackgroundImage:(UIImage*)image;
- (void) clearBackgroundImage;
@end


#import "UINavigationBar+CustomImage.h"

@implementation UINavigationBar (CustomImage)
- (void) setBackgroundImage:(UIImage*)image {
    if (image == NULL) return;
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    [self insertSubview:imageView atIndex:0];
    [imageView release];
}

- (void) clearBackgroundImage {    
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSArray *mySubviews = [self subviews];

    for (int i = [mySubviews count] - 1; i >= 0; i--)
    {
        if ([[mySubviews objectAtIndex:i] isMemberOfClass:[UIImageView class]])
        {
            [[mySubviews objectAtIndex:i] removeFromSuperview];
            return;
        }
    }

    [pool release];
}
@end

And i'm using the following code to generate the custom back button
in my view

UIButton *btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
[btnBack addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
[btnBack setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
UIBarButtonItem *barBtnBack = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
self.navigationItem.leftBarButtonItem = barBtnBack;

[btnBack release];
[barBtnBack release];

But the button most of the time is hidden under the bg image
and some times it randomly appears.
Why is this happening? I'm not sure what's the problem the image is inserted at index 0
so as I understand it is supposed to be behind all the time.
Please help.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

并安 2024-12-03 22:26:45

尝试以下代码来更改加载导航栏的类中导航栏的背景。

@interface UINavigationBar (MyCustomNavBar)
@end
@implementation UINavigationBar (MyCustomNavBar)
- (void) drawRect:(CGRect)rect 
{
    UIImage *barImage = [UIImage imageNamed:@"image.png" ];
    [barImage drawInRect:rect];
}
@end

Try the following code to change background of the navigation bar in the class where you are loading the navigation bar.

@interface UINavigationBar (MyCustomNavBar)
@end
@implementation UINavigationBar (MyCustomNavBar)
- (void) drawRect:(CGRect)rect 
{
    UIImage *barImage = [UIImage imageNamed:@"image.png" ];
    [barImage drawInRect:rect];
}
@end
疏忽 2024-12-03 22:26:45

使用 Praveen S 建议的方法,您可以为下一个背景图像名称设置一个全局变量,并将其设置在 viewDidLoad 方法中,然后在绘制矩形方法中使用存储在变量中的 imageName,而不是将其硬编码在绘制矩形中。

Using the method Praveen S suggested you could have a global variable for the next background image name and set it in your viewDidLoad method, then in your draw rect method use the imageName stored in the variable rather than hardcoding it in the draw rect.

流年已逝 2024-12-03 22:26:45

您应该在添加图像后尝试创建按钮。

You should try creating the button after adding the image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文