更改导航栏每个导航上的背景图像

Lal*_*wal 6 navigation iphone background image

我正在开发iPhone应用程序.我添加了导航栏背景图片

带接口: -

@interface UINavigationBar (backgroundImageWithTitle)

方法: -

- (void)drawRect:(CGRect)rect

通过这种方法,导航栏背景图像被设置一次.

我想从不同的.m文件中调用它来分配条形图上的不同图像.

如何实施?

提前致谢.

Vij*_*com 8

CustomNavigation.h

    #import <Foundation/Foundation.h>


    @interface UINavigationBar (UINavigationBarCustomDraw){

    }

    @end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

如果你想First.png在FirstViewController中设置navigationBar背景图像

在您的FirstViewController.m中

        -(void) viewWillAppear:(BOOL)animated{

        [super viewWillAppear:animated];



            self.title=@"First";
            [self.navigationController.navigationBar drawRect:CGRectMake(0, 0, 320, 480)];

    }
Run Code Online (Sandbox Code Playgroud)

如果你想Second.png在SecondViewController中设置navigationBar背景图像然后

在你的SecondViewController.m中

        -(void) viewWillAppear:(BOOL)animated{

        [super viewWillAppear:animated];



            self.title=@"Second";
            [self.navigationController.navigationBar drawRect:CGRectMake(0, 0, 320, 480)];

    }
Run Code Online (Sandbox Code Playgroud)