基于 Jorge 的代码,这是我的解决方案。
我创建了一个简单的类别UIViewController
:
UIViewController+ImageBackButton.h
#import <UIKit/UIKit.h>
@interface UIViewController (ImageBackButton)
- (void)setUpImageBackButton;
@end
UIViewController+ImageBackButton.m
#import "UIViewController+ImageBackButton.h"
@implementation UIViewController (ImageBackButton)
- (void)setUpImageBackButton
{
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 34, 26)];
[backButton setBackgroundImage:[UIImage imageNamed:@"back_arrow.png"] forState:UIControlStateNormal];
UIBarButtonItem *barBackButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
[backButton addTarget:self action:@selector(popCurrentViewController) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = barBackButtonItem;
self.navigationItem.hidesBackButton = YES;
}
- (void)popCurrentViewController
{
[self.navigationController popViewControllerAnimated:YES];
}
@end
现在您所要做的就是#import UIViewController+ImageBackButton.h
在您的所有视图控制器中或在您的其他视图控制器继承并实现该viewWillAppear:
方法的自定义基本视图控制器类中:
- (void)viewWillAppear:(BOOL)animated
{
[self setUpImageBackButton];
}
就这样。现在你到处都有一个图像后退按钮。无国界。享受!