用導(dǎo)航控制器 自定義返回按鈕時(shí)側(cè)滑返回手勢(shì)會(huì)失效
通常我們會(huì)在自定義了返回按鈕的控制器中去開啟手勢(shì)和設(shè)置代理
如果要修改很多頁(yè)面的話很麻煩
所以用runtime進(jìn)行全局修改。
新建UIViewController的類別
在.m文件中導(dǎo)入#import <objc/runtime.h>
然后添加如下代碼就可以了:
+ (void)load{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// 把viewDidLoad潜叛、viewDidAppear:絮爷、viewWillDisappear:替換成自定義的方法
Method viewDidLoadImp = class_getInstanceMethod(self, @selector(viewDidLoad));
Method cbi_viewDidLoadImp = class_getInstanceMethod(self, @selector(cbi_viewDidLoad));
method_exchangeImplementations(viewDidLoadImp, cbi_viewDidLoadImp);
Method viewDidAppearImp = class_getInstanceMethod(self, @selector(viewDidAppear:));
Method cbi_viewDidAppearImp = class_getInstanceMethod(self, @selector(cbi_viewDidAppear:));
method_exchangeImplementations(viewDidAppearImp, cbi_viewDidAppearImp);
Method viewWillDisappearImp = class_getInstanceMethod(self, @selector(viewWillDisappear:));
Method cbi_viewWillDisappearImp = class_getInstanceMethod(self, @selector(cbi_viewWillDisappear:));
method_exchangeImplementations(viewWillDisappearImp, cbi_viewWillDisappearImp);
});
}
- (void)leftItemClick {
[self.navigationController popViewControllerAnimated:YES];
}
//自定義的方法,進(jìn)行按鈕自定義欢峰、手勢(shì)的開關(guān)葬荷、手勢(shì)代理設(shè)置
- (void)cbi_viewDidLoad {
//不在一級(jí)界面時(shí)添加自定義按鈕
if ([self.navigationController.viewControllers count] > 1) {
//在這里自定義你需要的全局返回按鈕
UIBarButtonItem * leftItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"返回"] style:UIBarButtonItemStylePlain target:self action:@selector(leftItemClick)];
self.navigationItem.leftBarButtonItem = leftItem;
}
[self cbi_viewDidLoad];
}
- (void)cbi_viewDidAppear:(BOOL)animated {
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
if ([self.navigationController.viewControllers count] > 1) {
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
} else {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
}
[self cbi_viewDidAppear:animated];
}
- (void)cbi_viewWillDisappear:(BOOL)animated {
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
[self cbi_viewWillDisappear:animated];
}
在.m的這個(gè)方法中修改你的全局自定義按鈕:
- (void)cbi_viewDidLoad {
//不在一級(jí)界面時(shí)添加自定義按鈕
if ([self.navigationController.viewControllers count] > 1) {
//在這里自定義你需要的全局返回按鈕
UIBarButtonItem * leftItem = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:@"返回"]
style:UIBarButtonItemStylePlain
target:self
action:@selector(leftItemClick)];
self.navigationItem.leftBarButtonItem = leftItem;
}
[self cbi_viewDidLoad];
}
因?yàn)槭侨钟绊懙牟挥脤?dǎo)入頭文件就會(huì)生效。
如果想某個(gè)頁(yè)面不用側(cè)滑手勢(shì)返回
就在該控制器的viewDidAppear:
做如下操作關(guān)閉側(cè)滑手勢(shì):
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
如果要改變某個(gè)控制器的返回按鈕纽帖,直接在viewDidLoad
中自定義leftBarButtonItem
就好了宠漩,會(huì)覆蓋全局的。
代碼下載
GitHub:ZBCustomBackItem
下載后直接拖入項(xiàng)目中即可