Set the background image of the navigation bar for all bars

since I upgraded to xcode 4.2 and iOS 5, the 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 //.... 

To do this, apply the background image and color to the navigation bar. But since the update, I get the blue color by default. I use the tr20 structure in my project.

Any idea why this is not working? How to set a background image for all navigation panels in one place, and not on each controller?

+6
source share
5 answers

Try to install:

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

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


EDIT: Seems useful as well:

 [[UINavigationBar appearance] setTintColor:myColor]; 
+28
source

Ios5 has built-in methods for changing the background image. I created a category for this that works on ios 4 and 5. (Dont have the code here atm), but it should not be difficult to create it.

 [navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; 
+2
source

I had the same problem [[UINavigationBar appearance] setBackgroundImage:myImage forBarMetrics:UIBarMetricsDefault]; , which does not work on iOS 4, but works fine on iOS 5. Therefore, I tried an alternative 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]; } 
+1
source

If you already have an image installed on your UINavigationBar, and you want to change it while the application is running, do it.

 UIImage *NavigationPortraitBackground = [UIImage imageNamed:@"red_bg.png"]; [[UINavigationBar appearance] setBackgroundImage:NavigationPortraitBackground forBarMetrics:UIBarMetricsDefault]; [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"red_bg.png"] forBarMetrics:UIBarMetricsDefault]; 
+1
source

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

 [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault]; 
0
source

Source: https://habr.com/ru/post/900188/


All Articles