简体   繁体   中英

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)

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.

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 alloc] 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.

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

Simply Place that code in - (void)viewWillAppear:(BOOL)animated; so it'll work fine and add one image having size 320x40 named Top Bar

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

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

I encountered the same problem.Found out the best solution

 [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"background_image.png"] forBarMetrics:UIBarMetricsDefault];

Hope this would help....

Just write your own navigation bar. Therefore you have to disable the Navigation Bar fist:

Disable the top bar in the interface builder by selecting your Navigation Controller in your Storyboard: Attributes Inspector -> Simulated Metrics -> Top Bar: select None

Afterwards you can add any HeaderView you like...

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, sFrame.size.width, 100)];
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"header_image.png"]];
self.headerView.backgroundColor = background;

// ...add buttons and labels

[self.view addSubview:headerView];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM