I have a problem, I need to insert an image in a UIBarButtonItem, but it doesn't shows the button with the image.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//.........
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:menuVC];
UINavigationBar *barraNav = navVC.navigationBar;
[barraNav setBackgroundImage:[UIImage imageNamed:@"navigationBar.png"] forBarMetrics:UIBarMetricsDefault];
// HERE
UINavigationItem *navItem = [[UINavigationItem alloc] init];
navItem = navVC.navigationItem;
UIBarButtonItem *botonIzq = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bNavBarIzq.png"] style:UIBarButtonItemStylePlain target:self action:nil];
[navItem setLeftBarButtonItem:botonIzq];
self.window.rootViewController = navVC;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
This should work :
UIImage *image = [[UIImage imageNamed:@"bNavBarIzq.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *botonIzq = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(YOUR_METHOD:)];
A navigation item is not a feature of a navigation controller but of one of its children. It is the menuVC
whose navigation item should be set here.
Moreover, this should rightly be the job of menuVC
, ie the code should be in its class - this is not the job of the app delegate (though there is nothing strictly wrong with that, it's just a "proper division of labor" type of thing).
You are using wrong navigation item. You should use menuVC one, instead of navVC.
Just try:
UIBarButtonItem *botonIzq = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bNavBarIzq.png"] style:UIBarButtonItemStylePlain target:self action:nil];
menuVC.navigationItem.leftBarButtonItem = botonIzq;
Also, please, check this code:
// HERE
UINavigationItem *navItem = [[UINavigationItem alloc] init];
navItem = navVC.navigationItem;
You've created navItem, and then override it. You should refactor it.
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.