aug2uag
aug2uag

Reputation: 3445

Add image to UINavigationBar with tintColor

I'm trying to add an image to a UINavigationBar that has a custom color. I was hoping to avoid having to use a .png for the entire thing, and to programmatically set the color, and drop the icon on top of the colored navigation bar.

self.navigationController.navigationBar.tintColor = [UIColor redColor];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"icon.png"] forBarMetrics:UIBarMetricsDefault];

The UIBarMetricsDefault causes a repeat for the image icon, and I just want one in the center. The UIBarMetricsDefault and UIBarMetricsLandscapePhone settings are not working for me, and I was hoping someone would know a better suggestion.

I am trying to avoid using a .png for the entire navigation bar, and would like to add the image to a color that is set programmatically. Thank you!

Upvotes: 1

Views: 573

Answers (1)

Geek
Geek

Reputation: 8320

The way you set color is correct.
To set image in center use titleView property of navigationItem as shown below :

self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon.png"]];

Upvotes: 3

Related Questions