Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

iphone - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...