Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding image to navigation bar

I'd like an image to take up all of a navigation bar. This is the navigation that comes with a navigation based app. It appears on the RootViewController with the accompanying UITableView. I've seen some examples of how this might work.

Set navigation bar title:

UIImage *image = [UIImage imageNamed:@"TableviewCellLightBlue.png"]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; [self.navigationController.navigationBar.topItem setTitleView:imageView]; 

The problem there is it only covers the title rather than the entire navigation bar.

There is also this thread: http://discussions.apple.com/message.jspa?messageID=9254241#9254241. Towards the end, the solution looks to use a tab bar, which I'm not using. It is that complicated to set a navigation bar background? Is there some other simpler technique?

I'd like to have a background for the navigation and still be able to use title text.

like image 545
4thSpace Avatar asked Oct 12 '09 07:10

4thSpace


People also ask

How do I add a picture to navigation bar?

We just need to add a div tag with the class as a container and put the navbar-brand(image or logo) inside this div. After that, we just need to add the class mx-auto to the navbar-brand class.

How do I add items to the navigation bar?

Use any element to open the dropdown menu, e.g. a <button>, <a> or <p> element. Use a container element (like <div>) to create the dropdown menu and add the dropdown links inside it. Wrap a <div> element around the button and the <div> to position the dropdown menu correctly with CSS.


2 Answers

In your case, this solution found in another answer would work well.

With the "CustomImage" category added to UINavigationBar, you can then just call:

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

This code should go in the method

- (void)viewWillAppear:(BOOL)animated 

of the view controller where you want to have the custom image. And, in that case you should better call:

[navBar clearBackgroundImage]; // Clear any previously added background image 

before setBackgroundImage (otherwise it will be added multiple times...)

like image 178
Jean Regisser Avatar answered Sep 23 '22 00:09

Jean Regisser


its changed for ios6, to make it work in ios 6 use:

UINavigationBar *navBar = self.navigationController.navigationBar; UIImage *image = [UIImage imageNamed:@"image.png"]; [navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; 
like image 35
user513790 Avatar answered Sep 21 '22 00:09

user513790