如何用不同的图像完全替换后退按钮的图像?

时间:2011-07-07 19:14:22

标签: iphone

以下是我的编码

// Navigation logic may go here -- for example, create and push another view controller.
NextViewController *nextViewController = 
[[NextViewController alloc] initWithNibName:@"NextViewController" bundle:nil];
[self.navigationController pushViewController:nextViewController animated:YES];
[nextViewController release];

UIImage *backImage=[UIImage imageNamed:@"backbutton.png"]; 
UIBarButtonItem * backBarButtonItem = [[UIBarButtonItem alloc] 
                                       initWithImage:backImage style:UIBarButtonItemStylePlain target:nil 
                                       action:nil]; 
self.navigationItem.backBarButtonItem = backBarButtonItem; 
[backBarButtonItem release];

}

2 个答案:

答案 0 :(得分:1)

在这里,来自我自己的项目。 (我在viewDidLoad中这样做)

    UIImage *image = [UIImage imageNamed:@"btn-custom-back.png"];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:image forState:UIControlStateNormal];
    button.frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    [button addTarget:self action:@selector(popView) forControlEvents:UIControlEventTouchUpInside]; 

    UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height)];
    [buttonView addSubview:button];

    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:buttonView];
    [buttonView release];

    self.navigationItem.leftBarButtonItem = backItem;
    [backItem release];

方法:

- (void)popView {
    [self.navigationController popViewControllerAnimated:YES];
}

答案 1 :(得分:1)

在将新视图控制器推入导航堆栈之前,必须对后退按钮进行修改。

所以只需将代码移动到这样:

UIImage *backImage=[UIImage imageNamed:@"backbutton.png"]; 
UIBarButtonItem * backBarButtonItem = [[UIBarButtonItem alloc] 
                                   initWithImage:backImage style:UIBarButtonItemStylePlain target:nil 
                                   action:nil]; 
self.navigationItem.backBarButtonItem = backBarButtonItem; 
[backBarButtonItem release];

NextViewController *nextViewController = 
[[NextViewController alloc] initWithNibName:@"NextViewController" bundle:nil];
[self.navigationController pushViewController:nextViewController animated:YES];
[nextViewController release];