如何为导航栏添加背景图像?

时间:2010-12-13 09:04:46

标签: iphone uinavigationcontroller

我想为导航栏添加背景图片。我尝试了以下代码,但它隐藏了导航标题。

self.title = @"My title";
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top.png"]];
[navBar addSubview:image];
[navBar sendSubviewToBack:image];

任何伙伴都知道为什么会这样?

6 个答案:

答案 0 :(得分:3)

这应该是导航控制器初始化的地方,即MainViewController.m

// Set the background image of the navigation bar
UIImage *image = [UIImage imageNamed:@"banner-with-logo.png"];
[self.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

答案 1 :(得分:3)

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"background.png"] forBarMetrics:UIBarMetricsDefault];

答案 2 :(得分:0)

您可以通过覆盖drawRect方法

来完成
@implementation UINavigationBar (TENavigationBar)

- (void) drawRect:(CGRect)rect
{
    UIImage *image = [UIImage imageNamed:@"navigation_background"];

    [image drawInRect:CGRectMake(0, 
                                 0, 
                                 self.frame.size.width, 
                                 self.frame.size.height)];
}

@end

答案 3 :(得分:0)

WaiLam的解决方案可行,但如果您在应用中播放电影,则播放器导航栏也会发生变化。这是一个更好的方法 -

@implementation UINavigationBar (UINavigationBarCategory)

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

答案 4 :(得分:0)

这会将具有正确大小的图像添加到NavigationBar中心。

/*  custom navbar logo, centered with same height as navbar  */
UIImageView *logo = [[UIImageView alloc] initWithFrame:
        CGRectMake(self.navigationController.navigationBar.frame.size.width/2-75,0,150,
                   self.navigationController.navigationBar.frame.size.height-1)];
[logo setImage:[UIImage imageNamed:@"text_logo.png"]];
[self.navigationController.navigationBar addSubview:logo];
[self.navigationController.navigationBar sendSubviewToBack:logo];

答案 5 :(得分:0)

如果要添加图像,使其在整个应用程序中始终与使用此图像相同:

- (void)drawRect:(CGRect)rect {
    // Drawing code 
    UIImage *img = [UIImage imageNamed: @"background-image.png"];
    [img drawInRect:CGRectMake(0, 
                               0, 
                               self.frame.size.width, 
                               self.frame.size.height)];
}

否则,如果你想在特定的屏幕上设置图像,而不是像下面那样有点棘手:

- (void) viewWillAppear{
  ...
  ...
  [self addNavigationBarImage:YES];
}

- (void) viewWillDisappear{
  ...
  ...
  [self addTheNavigationBarImage:NO];
}

- (void) addTheNavigationBarImage:(BOOL)add{
  UIImageView *imgView = (UIImageView *)[[self.navigationController navigationBar] viewWithTag:TAG];
  if(add){
    if(imgView == nil){
      imgView = [[[UIImageView alloc] initWithImage:IMAGE] autorelease];
      [self.navigationController.navigationBar insertSubview:imgView atIndex:0];
    }
    //EDIT:
    else{
      [self.navigationController.navigationBar sendSubviewToBack:imgView];
    }

  }
  else{
   if(imgView)
      [imgView removeFromSuperview];
  }
}

希望这有帮助。