【问题标题】:UINavigationItem leftBarButton is hidden behind it's backgroundUINavigationItem leftBarButton 隐藏在它的背景后面
【发布时间】:2011-04-05 06:34:02
【问题描述】:

我有一个带有自定义背景的 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];

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

提前致谢。 瑞奇。

【问题讨论】:

    标签: cocoa-touch uinavigationcontroller uinavigationbar uinavigationitem


    【解决方案1】:

    你可以使用这个设置导航栏的背景图片

    @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
    

    【讨论】:

    • 感谢 saadnib,但如果我想更改整个应用程序的背景会怎样?
    • 抱歉,我的意思是我希望导航栏的背景在整个应用程序中更改为不同的图像?
    【解决方案2】:

    我最终做了以下事情。

    @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

    感谢您的帮助。

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 2018-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-07
      相关资源
      最近更新 更多