Set background image of navigation bar for all bars
Asked Answered
I

5

6

since I updated to xcode 4.2 and iOS 5 following code has no effect:

@implementation UINavigationBar (CustomNavBarBG)
-(void)drawRect:(CGRect)rect{
    UIImage *image = [UIImage imageNamed:@"navbar.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    self.tintColor = [UIColor colorWithRed:124/255.0 green:177/255.0 blue:55/255.0 alpha:1.0];
}
@end

@implementation MyAppDelegate
//....

This should apply a background image and color to the navigation bar. But since the update I get the default blue color. I use the three20 framework in my project.

Any idea why this does not work? How can I set the background image for all navigation bars at one place and not in every view controller?

Imray answered 27/10, 2011 at 10:1 Comment(1)
What about the update problems? #15024537Contrarily
V
28

Try setting:

[[UINavigationBar appearance] setBackgroundImage:myImage forBarMetrics:UIBarMetricsDefault];

in your - (void) applicationDidFinishLaunching:(UIApplication *)application


EDIT: Seems helpfull too:

[[UINavigationBar appearance] setTintColor:myColor]; 
Vladimir answered 27/10, 2011 at 10:15 Comment(4)
ok thanks that works, do you have an idea how I can set the tint color the same way? When I put setTintColor instead setBackgroundImage the method is not found...Imray
The matter effect I do! [[UINavigationBar appearance] setTintColor:myColor];Vladimir
actually this is what I do "[[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:124/255.0 green:177/255.0 blue:55/255.0 alpha:1.0]];" but the back buttons in the toolbar are still blueImray
for the back button you need a different setting again:D ill look it up for you later.Vladimir
G
2

There are native methods in ios5 to change the background image. I created a category for this that works on ios 4 & 5. (Dont have the code here atm) but should't be hard to create one.

[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
Gallicize answered 27/10, 2011 at 10:13 Comment(0)
Z
1

I had the same issue of [[UINavigationBar appearance] setBackgroundImage:myImage forBarMetrics:UIBarMetricsDefault]; which doesn't work on iOS 4 but was working fine on iOS 5. So I tried an alternate solution for it.

image = [UIImage imageNamed:@"yourImageName.png"];
    if([[UINavigationBar class] respondsToSelector:@selector(appearance)]) //iOS >=5.0
    {
        [[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    }
    else
    {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        [self.navigationController.navigationBar addSubview:imageView];
    }
Zendavesta answered 4/5, 2012 at 12:51 Comment(0)
P
1

I have another solution, because the solution above hiding all buttons on navigation bar

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];
Proser answered 20/4, 2013 at 13:22 Comment(0)
L
1

If you already have an image set on your UINavigationBar and you would like to change it while your app is running then do this.

UIImage *NavigationPortraitBackground = [UIImage imageNamed:@"red_bg.png"];
[[UINavigationBar appearance] setBackgroundImage:NavigationPortraitBackground forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"red_bg.png"] forBarMetrics:UIBarMetricsDefault];
Lawn answered 2/9, 2014 at 19:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.