Question

i want to change navigation bar background using my own image, but i really dont know what to do. i was search it on google, but some samples tell in navigationbar template. my application is a split view base. How could i do that??

Was it helpful?

Solution

If you want to change the background of the navigation bar that too without using category, this might be helpful to give that feel, see if it fulfills your need:

- (void)viewDidLoad {
    [super viewDidLoad];

        UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
        //here for v, width= navBar width and height=navBar height

    [v setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"a.png"]]];

    [self.view addSubview:v];

    [v release];

    [self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];

    [self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];
}

OTHER TIPS

#import "ImageViewController.h"

@implementation UINavigationBar (CustomImage)

- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed:@"background.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

@end
implement this code in your .m file


@implementation ImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    UIImageView *backGroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
    [self.navigationController.navigationBar insertSubview:backGroundView atIndex:0];
    [backGroundView release];
}

@end

You can add the image directly in the navigation bar background with the code below

[navigation_bar setBackgroundImage:[UIImage imageNamed:@"image.jpg"] forBarMetrics:UIBarMetricsDefault];

To add a background image to UINavigationBar, you need to create a category that extends UINavigationBar. Just find the below given code and add it into your implementation file.

@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: @"background.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

After implementing it.. you can call it anywhere within your application and you can see the change on every view. Do not forget to add image into your resources folder.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top