ios navigation bar backgroundimage - iphone

I have the following code that is supposed to set the background image on my navigation bar.
if ([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] )
{
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navigation-bar-background"] forBarMetrics:UIBarMetricsDefault];
}
else
NSLog(#"WTF");
It works on the iPad but not on an iPhone, on the iPhone it logs WTF ..... same result without the if statement.
The iPhone I have is iPhone 4 running ios 6.0, the iPad is iPad 2 with ios 6.0
Any help is appreciated.
Thanks,

You could try this alternate old method
#implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"navigation-bar-background.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end

Related

Change UINavigationBar background image to default - iOS 4.3

I change UINavigationBar background image with overloaded method
#implementation UINavigationBar (Background)
-(void) drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed:#"Header.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
But in some UIViewControllers I need to remove this background image. How I can do it?
You might look at this blog post from Sebastian Celis. It solves exactly the problem your facing.
And maybe also check this.
test in this way
+ (void)setNavigationbarBackground:(UINavigationBar *)nBar imageName:(NSString *)imgName {
if([nBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)]) {
[nBar setBackgroundImage:[UIImage imageNamed:imgName] forBarMetrics:UIBarMetricsDefault];
} else {
[nBar setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:imgName]]];
}
}

setBackgroundImage forBarMetrics + scaleToFill?

Since iOS5 you should set the backgroundimage of tool- or navigationbars via setBackgroundImage forBarMetrics.
In iOS4 I use:
#implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"NavigationBar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
In iOS4 the image "scales to fill" in iOS5 the image is repeated if it's to small. Is there a way to set the behaviour to "scaleToFill" when you use setBackgroundImage forBarMetrics?
Thx!

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];

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]];
}

using image or tint color on uinavigationbar in iphone?

how do i show a background image on a navigation bar or give tint color to the navigation bar in a native iphone application??
For iOS5 use the following lines of code:
UINavigationBar *navBar = [[self navigationController] navigationBar];
UIImage *backgroundImage = [UIImage imageNamed:#"nav-bar-background-normal"];
[navBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
For backward compatibility do a check to see if the navigation bar responds to setBackgroundImage:forBarMetrics:
More information on:
http://sebastiancelis.com/2009/12/21/adding-background-image-uinavigationbar/
This's how I did on iOS4:
#import <QuartzCore/QuartzCore.h> // For .layer
self.navigationController.navigationBar.layer.contents = (id)[UIImage imageNamed:#"navigationBarBackgroundImage"].CGImage;
self.navigationController.navigationBar.tintColor = [UIColor orangeColor];
No need to switch subviews between z-orders (-exchangeSubviewAtIndex:withSubviewAtIndex:), both background image and tintColor set in one line of code, and works with #2x image too.
Was looking for this a week ago. Found this over here discussions. apple. com/thread.jspa?threadID=1649012&tstart=0 (sorry won't let me post a real link).
-(void)setBackgroundImage:(UIImage*)image withTag:(NSInteger)bgTag{
if(image == NULL){ //might be called with NULL argument
return;
}
UIImageView *aTabBarBackground = [[UIImageView alloc]initWithImage:image];
aTabBarBackground.frame = CGRectMake(0,0,self.frame.size.width,self.frame.size.height);
aTabBarBackground.tag = bgTag;
[self addSubview:aTabBarBackground];
[self sendSubviewToBack:aTabBarBackground];
[aTabBarBackground release];
}
/* input: The tag you chose to identify the view */
-(void)resetBackground:(NSInteger)bgTag {
[self sendSubviewToBack:[self viewWithTag:bgTag]];
}
I made this as a category to UINavigationBar. To set it a background image for a UINavigationBar inside a UINavigationBarController, I did this:
[navigationControllerForChannels.navigationBar setBackgroundImage:[UIImage imageNamed:#"top_bar.png"] withTag:48151623];
I've had some buginess when updating the tab bar, so you'll want to call
[self.navigationController.navigationBar resetBackground:48151623];
After any modifications to the bar.
You can override UINavigationBar drawRect. The code can be placed to appDelegate.m
I've tested it and it's working on 3x and 4x iOS.
#implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIColor *color = [UIColor blackColor]; //tint color
UIImage *img = [UIImage imageNamed: #"navBarBg.png"]; // your image
[img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.tintColor = color;
}#end
For the iOS5 and iOS6 I've used this solutions and it worked perfectly, Making a Universal UINavigationBar Background Image.
iPhone Retina Portrait 640px x 88px / iPhone Non-Retina Portrait 320px x 44px
Inside AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Place this code
// Set the status bar to black color.
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque animated:NO];
// Change #"menubar.png" to the file name of your image.
UIImage *navBar = [UIImage imageNamed:#"menubar.png"];
[[UINavigationBar appearance] setBackgroundImage:navBar forBarMetrics:UIBarMetricsDefault];
Don't forget to change the image name (menubar.png)
Check out this link for the full answer http://www.lwxted.com/blog/2012/add-custom-background-image-uinavigationbar-ios-5/
a background image is going to take a bit more work (you might want to try setting a titleView that's the same size as the bar itself; I haven't tried this myself) or adding a view behind existing subviews. Tint color is easy: navBar.tintColor = [UIColor orangeColor];
If you use the CGImage solution, you may have a problem with image size:
CGRect layer=self.navigationController.navigationBar.frame;
layer.size.height=57.0;
layer.origin.y=0;
self.navigationController.navigationBar.frame=layer;
CGImageRef imageRef = [UIImage imageNamed:#"myImg.png"].CGImage;
self.navigationController.navigationBar.layer.contents = (id)imageRef;
It seems to me that the image is streched down, since the layer seems to have a height of 44.0 pixel, but a background image for the UINavigationBar should be at least 57.0.
If you try to move the layer's frame, all the buttons will move within it.