ナビゲーションコントローラーで使用しているすべての VC にカスタムビューコントローラークラスを使用することになりました。
OEViewController.h
#import <UIKit/UIKit.h>
#import "OEBarButtonItem.h"
@interface OEViewController : UIViewController
@property (nonatomic, retain) UIButton *backButton;
@property (atomic) BOOL pushed;
- (void)pushViewController:(UIViewController*)vc;
@end
OEViewController.m
#import "OEViewController.h"
#define ANIMATION_DURATION 0.33
@implementation OEViewController
@synthesize backButton, pushed;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
pushed=YES;
self.navigationItem.hidesBackButton = YES;
backButton = [OEBarButtonItem backButtonWithTitle:[(UIViewController*)[self.navigationController.viewControllers objectAtIndex:[self.navigationController.viewControllers count]-2] title]];
[backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (pushed) {
self.backButton.frame = CGRectMake(self.backButton.frame.origin.x+100,
self.backButton.frame.origin.y+6,
self.backButton.frame.size.width,
self.backButton.frame.size.height);
[UIView animateWithDuration:ANIMATION_DURATION
animations:^{
self.backButton.frame = CGRectMake(self.backButton.frame.origin.x-100,
self.backButton.frame.origin.y,
self.backButton.frame.size.width,
self.backButton.frame.size.height);
}];
pushed=NO;
} else {
self.backButton.frame = CGRectMake(self.backButton.frame.origin.x-100,
self.backButton.frame.origin.y,
self.backButton.frame.size.width,
self.backButton.frame.size.height);
[UIView animateWithDuration:ANIMATION_DURATION
animations:^{
self.backButton.frame = CGRectMake(self.backButton.frame.origin.x+100,
self.backButton.frame.origin.y,
self.backButton.frame.size.width,
self.backButton.frame.size.height);
}];
}
}
- (void)backButtonPressed:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
[UIView animateWithDuration:ANIMATION_DURATION
animations:^{
self.backButton.frame = CGRectMake(self.backButton.frame.origin.x+100,
self.backButton.frame.origin.y,
self.backButton.frame.size.width,
self.backButton.frame.size.height);
}];
}
- (void)pushViewController:(UIViewController*)vc {
[self.navigationController pushViewController:vc animated:YES];
[UIView animateWithDuration:ANIMATION_DURATION
animations:^{
self.backButton.frame = CGRectMake(self.backButton.frame.origin.x-100,
self.backButton.frame.origin.y,
self.backButton.frame.size.width,
self.backButton.frame.size.height);
}];
}
@end
これには、UIAppearance を使用するのではなく、iOS 4.0 以降で動作するという追加の利点があります。
@lxt の助けに感謝します!