2009-09-04 81 views
6

我已将以下代码应用于我的应用程序以更改导航栏图像。iPhone中的UINavigation酒吧背景

- (void)viewWillAppear:(BOOL)animated { 
[super viewWillAppear:animated]; 
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]]; 
[self setNavigationBarTitle]; 
} 
-(void)setNavigationBarTitle { 
UIView *aViewForTitle=[[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)] autorelease]; 
UIImageView *aImg=[[UIImageView alloc] initWithFrame:CGRectMake(-8, 0, 320, 45)]; 
aImg.image=[UIImage imageNamed:@"MyTabBG.png"]; 
[aViewForTitle addSubview:aImg]; [aImg release]; 
UILabel *lbl=[[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 305, 45)] autorelease]; 
lbl.backgroundColor=[UIColor clearColor]; lbl.font=[UIFont fontWithName:@"Trebuchet MS" size:22]; 
lbl.shadowColor=[UIColor blackColor]; [lbl setShadowOffset:CGSizeMake(1,1)]; 
lbl.textAlignment=UITextAlignmentCenter; lbl.textColor=[UIColor whiteColor]; [email protected]"Mobile Tennis Coach Overview"; 
[aViewForTitle addSubview:lbl]; 
[self.navigationItem.titleView addSubview:aViewForTitle]; 
} 

查看下面的图片。你可以看到我面临的问题。

alt text


alt text

我的应用程序的每个视图控制器具有以上方法来设置导航栏背景。

如何,当我推新视图控制器到我的应用程序。后退按钮将出现。

我需要后退按钮才会出现。但图像应该在后退按钮后面。

现在我在这里有点困惑。

你能帮我解答吗?

在此先感谢您与我分享您的知识。

非常感谢。

+0

嗨萨格尔,请问正常的按钮(即不后退按钮)渲染?它工作正常还是也有同样的问题? – h4xxr 2009-09-04 19:45:02

+0

在我以前的导航控制器 - 视图将消失的方法 - 我已经设置标题为后退 - self.navigationItem.title = @“返回”。 – 2009-09-04 20:30:50

回答

6

一个恼人的夜晚后,我发现了一个轻微的调整到这一点,如果你使用drawLayer。使用drawRect,当您播放视频或YouTube视频时,导航栏会被替换为图像。我读了几篇文章,这导致他们的应用程序被拒绝。

@implementation UINavigationBar (UINavigationBarCategory) 

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx 
{ 
    if([self isMemberOfClass:[UINavigationBar class]]) 
    { 
    UIImage *image = [UIImage imageNamed:@"navBarBackground.png"]; 
    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 

如果这篇文章是准确的,所有应罚款这种方法: http://developer.apple.com/iphone/library/qa/qa2009/qa1637.html

5

简而言之,苹果不支持修改UINavigationBar的结构。他们真的不希望你做你想做的事情。这是什么导致你所看到的问题。

请提交雷达请求该功能,以便它可以得到足够的关注在某些时候被正式添加。

说了这样的话,要解决这个问题,你可以使用-drawRect:方法为UINavigationBar添加一个类别,并在该方法中绘制背景图像。像这样将工作:

- (void)drawRect:(CGRect)rect 
{ 
    static UIImage *image; 
    if (!image) { 
    image = [UIImage imageNamed: @"HeaderBackground.png"]; 
    if (!image) image = [UIImage imageNamed:@"DefaultHeader.png"]; 
    } 
    if (!image) return; 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextDrawImage(context, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage); 
}