Sunday, April 8, 2012

how to display an image in the navigation bar of an iPhone application?


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



Source: Tips4all

4 comments:

  1. 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.

    ReplyDelete
  2. 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.

    ReplyDelete
  3. 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

    ReplyDelete
  4. If you want the image at the right 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];

    ReplyDelete