//
// UIViewController+BackButtonHandler.h
// ZHB
//
// Created by JackRen on 16/6/3.
// Copyright © 2016年 atense. All rights reserved.
//
#import <UIKit/UIKit.h>
@protocol BackButtonHandlerProtocol<NSObject>
@optional
-(BOOL)navigationShouldPopOnBackButton;
@end
@interface UIViewController (BackButtonHandler)<BackButtonHandlerProtocol>
@end
//
// UIViewController+BackButtonHandler.m
// ZHB
//
// Created by JackRen on 16/6/3.
// Copyright © 2016年 atense. All rights reserved.
//
#import "UIViewController+BackButtonHandler.h"
@implementation UIViewController (BackButtonHandler)
@end
@implementation UINavigationController (ShouldPopOnBackButton)
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem*)item {
if([self.viewControllers count] < [navigationBar.items count]) {
return YES;
}
BOOL shouldPop = YES;
UIViewController *vc = [self topViewController];
if([vc respondsToSelector:@selector(navigationShouldPopOnBackButton)]) {
shouldPop = [vc navigationShouldPopOnBackButton];
}
if(shouldPop) {
dispatch_async(dispatch_get_main_queue(), ^{
[self popViewControllerAnimated:YES];
});
} else {
for(UIView *subview in [navigationBar subviews]) {
if(subview.alpha < 1.) {
[UIView animateWithDuration:.25 animations:^{
subview.alpha = 1.;
}];
}
}
}
return NO;
}
@end