ホルヘのコードに基づいて、これが私の解決策です。
に簡単なカテゴリを作成します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];
}
それで全部です。これで、画像の戻るボタンがどこにでもあります。国境なし。楽しみ!