Question

UIImageView *navBarImageView = [[UIImageView alloc] initWithImage:navBarImage];
[navBarImageView setFrame:CGRectMake(0, 0, 320, 44)];
self.navigationItem.titleView = navBarImageView;
[navBarImageView release];

I am trying to add an image to my navigationBar, but when I used the code as listed above, it places the image into the navigationBar but leaves a grey gap to the left and right of the image. The image was created at 320x44, I'm not sure why it is resizing it despite the fact that I am setting the frame.

Was it helpful?

Solution

That's... probably not how you want to do that—it's not what the titleView is for. The usual way to do a custom navigation-bar background is to create a category on UINavigationBar, like so:

 @implementation UINavigationBar(MyCustomBackground)
 - (void)drawRect:(CGRect)r
 {
      [[UIImage imageNamed:@"my-navigation-background.png"] drawInRect:self.bounds];
 }
 @end

Throw that in a .m file in your project and you should see all of your navigation bars use "my-navigation-background.png" as their background.

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