문제

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;
}
도움이 되었습니까?

해결책

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, i.e. 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).

다른 팁

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

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top