Insert a image in UINavigationBar instead of text

2019-09-20 15:35发布

问题:

i have this piece of code which permits me to insert a title on the UIbar (if i'm not wrong) of the app.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ApplicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(goToAlbum) name:@"GetAlbumNotif" object:nil];
self.title = @"Events List";

Can you please give me an orientation to this? Thanks!

回答1:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourImage"]];
self.navigationItem.titleView = imageView;


回答2:

A UIBarButton is inside a NavigationBar. The easiest way is to add a UIBarButton to the NavigationBar in the Interface Builder. After that, you can alter the background with:

[barButton setBackgroundImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

You can change the appearance of a navigation bar if your deployment target is iOS 5.0 or higher with :

[UINavigationBar appearance] setBackgroundImage:

Or do the old fashion way with subclassing UINavigationBar and override drawInRect. How to do that can be found here: How to add background image on iphone Navigation bar?