R. Dewi
R. Dewi

Reputation: 4271

change navigation bar background using my own image

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??

Upvotes: 2

Views: 7036

Answers (4)

iPC
iPC

Reputation: 6168

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

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

Upvotes: 2

iOS Monster
iOS Monster

Reputation: 2119

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

Upvotes: 5

Swapna
Swapna

Reputation: 2235

#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

Upvotes: 2

marko
marko

Reputation: 1366

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.

Upvotes: 0

Related Questions