UINavigationItem leftBarButton 隐藏在其背景后面

发布于 2024-10-30 11:16:20 字数 933 浏览 0 评论 0原文

我有一个带有自定义背景和我自己的后退按钮的 UINavigationBar。我做了以下工作来尝试实现这一目标。

//custom background
UIImageView *background = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navigation-bar.png"]] autorelease];
[self.navigationController.navigationBar insertSubview:background atIndex:0];

//custom back button
UIImage *buttonImage = [UIImage imageNamed:@"btn_back.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:@selector(backPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
[customBarItem release];

后退按钮始终有效,但只有在我不应用自定义背景时才能看到。有人可以指出如何实现两者吗?

提前致谢。 瑞奇。

I have a UINavigationBar with a custom background and my own back button. I did the following to try achieve this.

//custom background
UIImageView *background = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navigation-bar.png"]] autorelease];
[self.navigationController.navigationBar insertSubview:background atIndex:0];

//custom back button
UIImage *buttonImage = [UIImage imageNamed:@"btn_back.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:@selector(backPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
[customBarItem release];

The back button always works, but it can only be seen if I don't apply my custom background. Could someone please point out how to achieve both?

Thanks in advance.
Ricky.

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

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

发布评论

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

评论(2

開玄 2024-11-06 11:16:20

您可以使用此设置导航栏的背景图像

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx 
{
    if([self isMemberOfClass:[UINavigationBar class]])
    {
        UIImage *image;


        image=[UIImage imageNamed:@"nav_bar_img"];


        CGContextClip(ctx);
        CGContextTranslateCTM(ctx, 0, image.size.height);
        CGContextScaleCTM(ctx, 1.0, -1.0);
        CGContextDrawImage(ctx,
                           CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage); 
    }
    else 
    {        
        [super drawLayer:layer inContext:ctx];     
    }
}  
@end

You can set background image of navigation bar by using this

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx 
{
    if([self isMemberOfClass:[UINavigationBar class]])
    {
        UIImage *image;


        image=[UIImage imageNamed:@"nav_bar_img"];


        CGContextClip(ctx);
        CGContextTranslateCTM(ctx, 0, image.size.height);
        CGContextScaleCTM(ctx, 1.0, -1.0);
        CGContextDrawImage(ctx,
                           CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage); 
    }
    else 
    {        
        [super drawLayer:layer inContext:ctx];     
    }
}  
@end
荒芜了季节 2024-11-06 11:16:20

我最终做了以下事情。

@implementation UINavigationBar (Category)

- (void)drawRect:(CGRect)rect
{
    MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
    [delegate.navImage drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

@end

每次我想要更改栏的背景时,我都会更改委托的 navImage 属性并在导航栏上调用 -setNeedsDisplay

感谢您的帮助。

I ended up doing the following.

@implementation UINavigationBar (Category)

- (void)drawRect:(CGRect)rect
{
    MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
    [delegate.navImage drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

@end

Every time I want to change the background of the bar, I change the delegate's navImage property and call -setNeedsDisplay on my navigation bar.

Thanks for your help.

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