开发者

different custom color for uinavigationbar and items

In my app I am overriding the uinavigationbar color in the AppDelegate to create this color across the entire app:

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawRect:(CGRect)rect {

UIColor *color = [UIColor colorWithRed:0.16
                                 green:0.20
                                  blue:0.32
                                 alpha:1];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColor(context, CGColorGetComponents( [color CGColor]));
CGContextFillRect(context, rect);
[self setBarStyle:UIBarStyleBlack];
[self setTintColor:color];


}

However, in one of my views, i would like to change the color of one of the nav bar items to another color, different from the global color above, but only for one of the items - the bar color should stay the same (reasoning - i'd like to have a nav bar item in green and a "On" text and change it to red with an "Off" text based on the user input).

I tried to开发者_运维问答 override the color of the button in my view in the following way, but it doesnt seem to do anything.

- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
    [self.navigationController.navigationBar setTintColor:[UIColor greenColor]];
}

Does anyone have a suggestion (or a better way) to make this happen?

Cheers.


It is tested code which will work 100%.

//in below code u can set the different image for different color

or

simply

fill the color by your question code.

here below is for put image in navigation bar

u can customize by removeing the picture code and puttting ur above code with different color.that it .logic is same

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜