2010-12-13 58 views
0

我想把導航欄的背景圖像。我嘗試了下面的代碼,但它隱藏了導航的標題。如何把導航欄的背景圖像?

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

任何哥們知道這是爲什麼?

+2

可能重複http://stackoverflow.com/questions/4319112/how-do-i-change-background-of-uinavigationbar) – 2010-12-13 09:13:43

+0

@middaparka它可能是一個可能的重複但不完全。請檢查我的答案並考慮它。 – 2010-12-13 10:24:12

回答

3
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"background.png"] forBarMetrics:UIBarMetricsDefault]; 
的[我如何改變一個UINavigationBar的背景?](
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 
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 
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]; 
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]; 
    } 
} 

希望這有助於。

+0

謝謝你回答我的問題。但是我在這段代碼中遇到了一些問題。 UIImageView * imgView = [self.navigationController.navigationBar viewWithTag:TAG];在標籤設置導航欄或任何圖像視圖的TAG位置。我爲導航欄設置了標記,然後在這裏使用它,但它會給出警告。 – 2010-12-14 09:30:50

+0

@Viral \ Narshana:您應該使用一個唯一的整數來代替TAG。 aand抱歉,你還應該類型化視圖:UIImageView * imgView =(UIImageView *)[[self.navigationController navigationBar] viewWithTag:1001] – 2010-12-14 10:17:45

+0

我得到了答案,但一個問題是圖像隱藏導航欄的標題和按鈕。我試過這個代碼,但它不起作用。 [self.navigationController.navigationBar sendSubviewToBack:imageView];對此有何想法? – 2010-12-15 05:12:05

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];