每个导航更改导航栏的背景图片 [英] Change Navigation bar Background image on each navigation

查看:237
本文介绍了每个导航更改导航栏的背景图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的iPhone应用程序。我已经加入导航栏背景图片

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

使用界面: -

@interface UINavigationBar的(backgroundImageWithTitle)

和方法: -

- (空)的drawRect:(的CGRect)RECT

通过这种方法,导航条的背景图片被设置一次。

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

我想从不同的 .M 的文件称其对酒吧分配不同的图像。

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

如何能实现?

先谢谢了。

推荐答案

CustomNavigation.h

CustomNavigation.h

    #import <Foundation/Foundation.h>


    @interface UINavigationBar (UINavigationBarCustomDraw){

    }

    @end

CustomNavigation.m

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

如果ü要First.png设置导航栏背景图片FirstViewController然后

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

在乌尔FirstViewController.m

in ur FirstViewController.m

        -(void) viewWillAppear:(BOOL)animated{

        [super viewWillAppear:animated];



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

    }

如果ü要Second.png设置导航栏背景图片SecondViewController然后

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

在乌尔SecondViewController.m

in ur SecondViewController.m

        -(void) viewWillAppear:(BOOL)animated{

        [super viewWillAppear:animated];



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

    }

这篇关于每个导航更改导航栏的背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆