Ramesh
Ramesh

Reputation: 43

Change the Background image in UINavigationBar

I want to change my background image in Navigation Bar.in my code below

UINavigationBar *navBar = self.navigationController.navigationBar;

UIImageView *image1=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];

image1.image = [UIImage imageNamed:@"image2_ram.jpg"];
[navBar setBackgroundImage:image1.image forBarMetrics:UIBarMetricsDefault];

image is changed but height of the image is not changed why?

Upvotes: 0

Views: 480

Answers (1)

Charan
Charan

Reputation: 4946

You should not set like that , if you are considering iOS 4.0 also

use this

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

or for iOS 5

use this, no need to give frame to it, and you cant change the height more than 44, the default height of navigation bar is 44

    UINavigationBar *navBar = self.navigationController.navigationBar;
    UIImage *image = [UIImage imageNamed:@"bgTopBar.png"];
    [navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

Upvotes: 2

Related Questions