tags:

views:

4665

answers:

4

how to display an image in the navigation bar of an iPhone application? (say, right after the title)

+2  A: 

the navigation bar has a property called title view - set this to the image you like. Since the titleView overwrites the title of the nav bar you have to include the desired title in the image file. Still set the title to what you want so it appears on the back button when you push a view Controller

zPesk
I don't think you would want to do this. Instead of making a simple 32x32 image (for example) next to text (extremely easy to draw), you're creating a much bigger image. But yes, this is definitely an option.
David McGraw
I have drawn an image 23x23, but do not know how to show it in the navigation bar. Could you let us know?
ebaccount
You don't have to replace title view, you can create a UIImageView, load your icon image, and add it as a child of the title view. Use the frame property of your UIImageView to position it where you want it.
Don McCaughey
+1  A: 

I haven't tested this but as UINavigationBar is a view you can add subViews to it.

UIImage* myImage = [UIImage imageNamed:@"Myimage.png"];
UIImageView* myImageView = [UIImageView initWithImage:myImage];
myImageView.frame.origin.x = 30.0; // You will have to find suitable values for the origin
myImageView.frame.origin.y = 5.0;

[myTabbar addSubView:myImageView];
[myImageView release];

You can use things like the backItem property to calculate the position of your image view.

Roger Nolan
Hello, Could you let me know how can I get access to my UINavigationBar?Thanks.
ebaccount
Yes, it works. It shows the image at the left-top corner of the navigation bar. I got the access by UINavigationBar *navBar = [self.navigationController navigationBar];However, I could not control the x, y position of the image. No matter what values I set at myImageView.frame.origin.x and y it still shows at the top left corner. Could you let me know how can I set the position
ebaccount
Can you show the code so I can see exactly what you are doing?Remember that your images frame is relative to the UITabbar's frame so you should check that first.
Roger Nolan
+4  A: 

Here's how to have the image centered in the NavBar.

UIImage *image = [UIImage imageNamed: @"NavBarImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];

self.navigationItem.titleView = imageView;

[imageView release];

This code is actually contained with the Apple source for the NavBar and can be found at the iPhone Developer Center at Apple. The source shows you various ways to manipulate both the StatusBar & NavBar.

Magnatek
You may want to set the size of the imageView prior to setting it the titleView. The origin parameters don't matter -- it'll be centered automatically. The second two numbers are width and height.`imageView.frame = CGRectMake(0, 0, 200, 30);`
Mickey Ristroph
thanks this worked perfect!
iWasRobbed
+1  A: 

If you want the image at the left of the nav bar, you can define it as a custom button with no action when presed, like this

UIButton* fakeButton = (UIButton *) [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage.png"]];
UIBarButtonItem *fakeButtonItem = [[UIBarButtonItem alloc] initWithCustomView:fakeButton];
self.navigationItem.rightBarButtonItem = fakeButtonItem;
[fakeButtonItem release];
[fakeButton release];
Donal O'Danachair
Yep, I think this one actually answers the original question (having the image to the right of the title). That's just what I needed, too!
Darren Oster