Skip to content

Instantly share code, notes, and snippets.

@loiclefloch
Created May 22, 2015 10:56
Show Gist options
  • Save loiclefloch/fadb78292831c22c6108 to your computer and use it in GitHub Desktop.
Save loiclefloch/fadb78292831c22c6108 to your computer and use it in GitHub Desktop.
iOS change previous button globally
#import "UIViewController+BackButton.h"
#import <objc/runtime.h>
@implementation UIViewController (PMTBackButton)
+(void)load
{
Method viewWillAppear = class_getInstanceMethod(self, @selector(customViewWillAppear:));
Method customViewWillAppear = class_getInstanceMethod(self, @selector(viewWillAppear:));
method_exchangeImplementations(viewWillAppear, customViewWillAppear);
}
-(void)customViewWillAppear:(BOOL)animated{
[self customViewWillAppear:animated];
if ([self.navigationController.viewControllers indexOfObject:self] != 0 && !self.navigationItem.hidesBackButton){
UIBarButtonItem *previousBarButton = nil;
UIButton* previousButton = [UIButton buttonWithType:UIButtonTypeCustom];
[previousButton addTarget:self action:@selector(popViewControllerWithAnimation) forControlEvents:UIControlEventTouchUpInside];
[previousButton setImage:[UIImage imageNamed:@"back_btn.png"] forState:UIControlStateNormal];
previousButton.frame = CGRectMake(0, 0, 20, 20);
// [cancelButton sizeButtonToFit];
previousBarButton = [[UIBarButtonItem alloc] initWithCustomView:previousButton];
NSMutableArray * leftButtons = [NSMutableArray arrayWithObject:previousBarButton];
[leftButtons addObjectsFromArray:self.navigationItem.leftBarButtonItems];
[self.navigationItem setLeftBarButtonItem:nil];
[self.navigationItem setLeftBarButtonItems:leftButtons];
}
[self.navigationItem setHidesBackButton:YES];
}
-(void)popViewControllerWithAnimation{
[self.navigationController popViewControllerAnimated:YES];
}
@endb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment