Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 953
testing Avatar asked Aug 25 '10 16:08

testing


People also ask

How do I darken my navigation bar?

Tap on the settings cog next to the static color icon — this brings up a color wheel to choose your desired shade. Select black for your saturation and zero opacity, then tap on Select This color . You should notice your nav bar turn black at this point.

How do I change the color of the home bar on my Android?

Step 1: After opening the android studio and creating a new project with an empty activity. Step 2: Navigate to res/values/colors. xml, and add a color that you want to change for the status bar. Step 3: In your MainActivity, add this code in your onCreate method.


3 Answers

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.

like image 51
Joshua Avatar answered Oct 20 '22 20:10

Joshua


Try this in Interface Builder on your UINavigationController.

enter image description here

like image 28
Mohammad Zaid Pathan Avatar answered Oct 20 '22 22:10

Mohammad Zaid Pathan


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

like image 35
Desert Rose Avatar answered Oct 20 '22 21:10

Desert Rose