Change Navigation bar Background image on each navigation

后端 未结 6 1729
梦如初夏
梦如初夏 2020-12-09 13:38

I am working on iPhone app. I have added Navigation bar Background image

With interface: -

@interface UINavigationBar (backgroundImageWithTitle)

相关标签:
6条回答
  • 2020-12-09 14:03

    CustomNavigation.h

        #import <Foundation/Foundation.h>
    
    
        @interface UINavigationBar (UINavigationBarCustomDraw){
    
        }
    
        @end
    

    CustomNavigation.m

        @implementation UINavigationBar (UINavigationBarCustomDraw)
    
        - (void) drawRect:(CGRect)rect {
    
            [self setTintColor:[UIColor colorWithRed:0.5f
                                               green: 0.5f
                                                blue:0 
                                               alpha:1]];
    
            if ([self.topItem.title length] > 0) {
    
    
                if ([self.topItem.title isEqualToString:@"First"]) {
    
                    [[UIImage imageNamed:@"First.png"] drawInRect:rect];
    
                }
    
                else if ([self.topItem.title isEqualToString:@"Second"]) {
    
                    [[UIImage imageNamed:@"Second.png"] drawInRect:rect];                   
    
                }
    
    
    
    
                CGRect frame = CGRectMake(0, 0, 320, 44);
                UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
                [label setBackgroundColor:[UIColor clearColor]];
                label.font = [UIFont boldSystemFontOfSize: 20.0];
                label.shadowColor = [UIColor colorWithWhite:0.0 alpha:1];
                label.textAlignment = UITextAlignmentCenter;
                label.textColor = [UIColor whiteColor];
                label.text = self.topItem.title;
                self.topItem.titleView = label;
    
    
    
    
    
            } 
    
    
            else {
                [[UIImage imageNamed:@"wood.png"] drawInRect:rect];
                self.topItem.titleView = [[[UIView alloc] init] autorelease];
            }
    
    
    
        }
    
        @end
    

    if u want to First.png to set navigationBar background image in FirstViewController then

    in ur FirstViewController.m

            -(void) viewWillAppear:(BOOL)animated{
    
            [super viewWillAppear:animated];
    
    
    
                self.title=@"First";
                [self.navigationController.navigationBar drawRect:CGRectMake(0, 0, 320, 480)];
    
        }
    

    if u want to Second.png to set navigationBar background image in SecondViewController then

    in ur SecondViewController.m

            -(void) viewWillAppear:(BOOL)animated{
    
            [super viewWillAppear:animated];
    
    
    
                self.title=@"Second";
                [self.navigationController.navigationBar drawRect:CGRectMake(0, 0, 320, 480)];
    
        }
    
    0 讨论(0)
  • 2020-12-09 14:04

    Using (SDK 3.2.5.) - In one of my Apps I needed navigationBar with custom image both in landscape and portrait.

    What I did was:

    @implementation UINavigationBar (BackgroundImage)
    
    - (void)drawRect:(CGRect)rect 
    {
        UIImage *image;
    
        if(self.frame.size.width == 320) //for iPhone - portrait will have 320 pix width.
        {
            image = [UIImage imageNamed: @"portraitNavigationBar.png"];
        }
        else
        {
            image = [UIImage imageNamed: @"landscapeNavigationBar.png"];
        }
    
        [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    }
    
    @end
    

    Luckily my app supports rotation - so, when rotating - navigation bar automatically redraws itself.

    But to manually redraw it - you can also call :

    [navigationController.navigationBar setNeedsDisplay];
    
    0 讨论(0)
  • 2020-12-09 14:06

    I have tried this

    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"name"] forBarMetrics:UIBarMetricsDefault];

    It works fine for iOS 5.0 but got crashed in iOS 4.3 or below.

    You have either set conditional i.e for

    iOS 5

    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"name"] forBarMetrics:UIBarMetricsDefault];

    else

    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"name"]];

    but still there is issue that it shows line under image.

    Or you can do in this way

    self.navigationItem.hidesBackButton = YES; 
    UINavigationBar *navBar = self.navigationController.navigationBar;
    
    UIImage *image = [UIImage imageNamed:@"image.png"];
    UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
    imgView.image = image;
    [tempView addSubview:imgView];
    [navBar clearBackgroundImage];
    [navBar addSubview:tempView];
    [tempView release];
    
    0 讨论(0)
  • 2020-12-09 14:09

    You can try in this way

    if([[UINavigationBar class] respondsToSelector:@selector(appearance)]) //iOS >=5.0
    {
        //[[UINavigationBar appearance] setFrame:CGRectMake(0, 0, 320, 40)];
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationBar.png"] forBarMetrics:UIBarMetricsDefault];
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navBar-Landscape.png"] forBarMetrics:UIBarMetricsLandscapePhone];   
    
        [[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];
    }
    

    and in else case you can add previous code i.e setBackgroundImage ....

    0 讨论(0)
  • 2020-12-09 14:14

    On iOS 5 this will not work but the good news is that there is a simple way of doing this

    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"name"] forBarMetrics:UIBarMetricsDefault];
    

    NOTE: This will only work on iOS 5 and above so make sure you check iOS version if you want to be backwards compatible.

    0 讨论(0)
  • 2020-12-09 14:24

    Or other option so that code runs only incase of iOS 5

    if([[UINavigationBar class] respondsToSelector:@selector(appearance)]) //iOS >=5.0
    {
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationBar.png"] forBarMetrics:UIBarMetricsDefault];
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navBar-Landscape.png"] forBarMetrics:UIBarMetricsLandscapePhone];   
    
    }
    
    0 讨论(0)
提交回复
热议问题