testing
testing

Reputation: 20279

change background color of navigation item (bar)

is there an easy way to change the background color of the navigation item on top of a view? I have a navigation based app and I only want that one view gets another background color. I created the views mainly with IB. I found the following solution (not tested):

float r=10;
float g=55;
float b=130;
float a=0;

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
[label setBackgroundColor:[UIColor colorWithRed:r/255.f
                                          green:g/255.f
                                           blue:b/255.f    
                                          alpha:a/255.f];];
[self.navigationController.navigationBar.topItem setTitleView:label];
[label release];

Is there an easier way like

[navigationController.navigationBar setBackgroundColor:blueColor]

Or can I do it with IB (but without having the same color in every view)?

Many thanks in advance!

Cheers

Upvotes: 7

Views: 33517

Answers (5)

Zaid Pathan
Zaid Pathan

Reputation: 16820

Try this in Interface Builder on your UINavigationController.

enter image description here

Upvotes: 3

Desert Rose
Desert Rose

Reputation: 3424

[[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:107.0/256.0 green:145.0/256.0 blue:35.0/256.0 alpha:1.0]]; Will change the color of the navigation bar of whole app.

Just place it in Appdelegate's didFinishLauncing method.

Upvotes: 2

jignesh
jignesh

Reputation: 41

@jerry

tint color is not supported to the RGB Coordinates

Upvotes: 1

Joshua
Joshua

Reputation: 15500

You could use:

[navigationController.navigationBar setTintColor:[UIColor redColor]; //Red as an example.

This would tint the color of the Navigation Bar and all it's Buttons to a specific color, in this case red. This property can also be set in Interface Builder.

And if you wanted to customize it further, you can set the background of the UINavigationBar to an image by sub-classing it. Like so…

Header File.

#import <UIKit/UIKit.h>

@interface UINavigationBar (CustomImage)

@end

Implementaion File.

#import "CustomNavigationBar.h"

@implementation UINavigationBar (CustomImage)

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx

{
    if([self isMemberOfClass: [UINavigationBar class]]){
        UIImage *image = [UIImage imageNamed:@"bar.png"];
        CGContextClip(ctx);
        CGContextTranslateCTM(ctx, 0, image.size.height);
        CGContextScaleCTM(ctx, 1.0, -1.0);
        CGContextDrawImage(ctx, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage);
    }else{
        [super drawLayer:layer inContext:ctx];
    }

}

@end

Then in Interface Builder set the class of you UINavigationBar to (in this case) CustomNavigationBar under the Identity Tab.

Upvotes: 18

Jerry Jones
Jerry Jones

Reputation: 5403

You can use navigationController.navigationBar.tintColor;

Upvotes: 1

Related Questions