Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Navigation bar Background image on each navigation

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

With interface: -

@interface UINavigationBar (backgroundImageWithTitle)

And method: -

- (void)drawRect:(CGRect)rect

By this method Navigation bar background images is being set one time.

I want to call it from different .m files for assigning different images on bar.

How it can be implemented?

Thanks in advance.

like image 863
Lalit Paliwal Avatar asked Jul 14 '11 11:07

Lalit Paliwal


People also ask

How do I add a picture to my navigation bar?

We just need to add a div tag with the class as a container and put the navbar-brand(image or logo) inside this div. After that, we just need to add the class mx-auto to the navbar-brand class.


3 Answers

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

    }
like image 186
Vijay-Apple-Dev.blogspot.com Avatar answered Oct 15 '22 14:10

Vijay-Apple-Dev.blogspot.com


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.

like image 44
Gil Margolin Avatar answered Oct 15 '22 14:10

Gil Margolin


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

}
like image 1
Kamleshwar Avatar answered Oct 15 '22 14:10

Kamleshwar