How do I change the background of a UINavigationBar? - iphone

I'd like to change the background of my UINavigationBar to a [UIColor colorWithImage:], but it isn't working. What am I missing?
EDIT:
Once I've created my subclass, where do I set the UINavigationController to use it?

You can use the tintColor property to change the colour of a UINavigationBar, but to set an image as the background you'll have to provide your own UINavigationBar subclass and override the drawRect: method, for example:
- (void)drawRect:(CGRect)rect {
// Drawing code
UIImage *img = [UIImage imageNamed: #"background-image.png"];
[img drawInRect:CGRectMake(0,
0,
self.frame.size.width,
self.frame.size.height)];
}
If you use Interface Builder to build your UI then to use the custom navigation bar, just select the UINavigationBar element in Interface Builder, open the Inspector and in the Identity tab specify your UINavigationBar subclass in the class field, like so:

To have an image in the navigation bar, you have to draw it yourself, which actually isn't that hard. Save this as UINavigationBar+CustomBackground.m (it adds a custom category to UINavigationBar):
#implementation UINavigationBar (CustomBackground)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:#"NavMain.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end

Related

Semitransparent background image (PNG24) for UINavigationBar only shows transparent after rotation

I'm using a category to implement a custom background for UINavigationBar.
#implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect
{
UIImage *img = [UIImage imageNamed: #"TopNav-YellowRule.png"];
[img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
The image that I am using is a semitransparent PNG24. When the app loads the background of the UINavigationBar shows up just fine but is not semitransparent - I cannot see the views that are below the UINavigationBar.
When I rotate the device, however, the image is suddenly semitransparent and works just fine. Any ideas what is happening on rotate that is allow the image to display as intended? Is there a way to fix it so that it displays properly on initial load, before a rotation?
Try adding this after #end. See if it helps
navgationController.navigationBar.backgroundColor = [UIColor clearColor];

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
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.
Try this in Interface Builder on your UINavigationController.
[[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.
You can use navigationController.navigationBar.tintColor;
#jerry
tint color is not supported to the RGB Coordinates

iPhone: UITabBar custom image doesn't work

I use the following UITTabBar and UINavigationBar built-in code to place my own custom image on the background of Tabbar and Navigation bar. It works great in my iPhone 3.0 application. Now, i am trying to convert my iPhone 3.0 application to iOS4.0. I ran the same project in Xcode 3.2.3(for iOS 4.0) now but unable to view my custom Tabbar image in the background of Tabbar. I tried debugging it, but it is not calling to this function UITabbar at all, but the same time it calls UINavigationBar well and works. Is this function(UITabbar) not there (or) can't use in iOS 4.0?
#implementation UITabBar(CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"Tabbar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
#implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"NavigationImage.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
Please advise.
Thank you.
I have resolved it by adding the code below:
[self.tabBarController.tabBar insertSubview:[ [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Tabbar.png"]] autorelease] atIndex:0];

Adding background Image in UINavigationBar

Is there a way to add an image in the background of UINavigationBar. There is no direct option through interface builder but I have seen apps implemented this thing.
Any help would be appreciated.
Thanks
Create a UIView and add it as a subview.
Edit: You can now use setBackgroundImage:forBarMetrics:. Source: https://stackoverflow.com/a/7765102/313875
Use this code
UIImage *backgroundImage = [UIImage imageNamed:#"strip.png"];
[upnavbar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
this wil work.
Just add your background as a subview, as jrtc27 suggested and change the tint color according your needs.
You can also override the drawLayer:inContext: method in a UINavigationBar category class. Inside the drawLayer:inContext: method, you can draw the background image you want to use. You can also use different sized images for portrait and landscape orientations if you'd like to.
- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
if ([self isMemberOfClass:[UINavigationBar class]] == NO) {
return;
}
UIImage *image = (self.frame.size.width > 320) ?
[UINavigationBar bgImageLandscape] : [UINavigationBar bgImagePortrait];
CGContextClip(context);
CGContextTranslateCTM(context, 0, image.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage);
}
And as a complete demo Xcode project on customizing the appearance of UINavigationBar this and this might be helpful.
I recently wrote an article about customization of background for UINavigatioBar and UIToolbar. You can find a code sample and categories for seamless integration of this functionality in your app by following link — http://leonov.co/2011/04/uinavigationbar-and-uitoolbar-customization-ultimate-solution/
You can also override the drawRect function.
#implementation UINavigationBar (UINavigationBarCustom)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:#"nav_bar_logo.png"];
[image drawInRect:rect];
}
#end

iPhone Navigation Bar Images

I'd like to replace the back button and the entire navigation bar in my iPhone app with custom images. First of all, is this possible? Second, if so, how? Thanks.
This code will modify every UINavigationBar in your app.
#implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"MyNavigationBar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
You won't be able to override the look and feel of Apple's navigation bar easily, and even if you are able to, there's a good chance it will go against their policy of avoiding private API's. However, it is pretty simple to create your own look-alike navigation bar using all custom images. This would work well for simple one-off screens that need the navigation bar, but will be a beast to implement on a complex UINavigationController-based hierarchy.
Edit: It looks like people have been able to change the background by overriding the -drawRect selector: Custom background for UINavigationBar? . I don't see any reports of whether or not the changes were approved for the App Store.
You can use this code to change navigation bar image and tab bar image to your custom images:
if([[UINavigationBar class] respondsToSelector:#selector(appearance)]) {//iOS >=5.0
UIImage *image = [UIImage imageNamed:#"myCustomImage.png"];
CGSize newSize = CGSizeMake(self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height);
UIGraphicsBeginImageContext(newSize);
[image drawInRect: CGRectMake(0, 0, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.navigationController.navigationBar setBackgroundImage:newImage forBarMetrics:UIBarMetricsDefault];
[[UIToolbar appearance] setBackgroundImage:newImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
[[UITabBar appearance] setBackgroundImage:newImage];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor cyanColor]];
}