├── UIViewController+BackButtonHandler.h ├── UIViewController+BackButtonHandler.m └── UIViewController-BackButtonHandler.podspec /UIViewController+BackButtonHandler.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIViewController+BackButtonHandler.h 3 | // 4 | // Created by Sergey Nikitenko on 10/1/13. 5 | // Copyright 2013 Sergey Nikitenko. All rights reserved. 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in 15 | // all copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | // THE SOFTWARE. 24 | // 25 | 26 | #import 27 | 28 | @protocol BackButtonHandlerProtocol 29 | @optional 30 | // Override this method in UIViewController derived class to handle 'Back' button click 31 | -(BOOL)navigationShouldPopOnBackButton; 32 | @end 33 | 34 | @interface UIViewController (BackButtonHandler) 35 | 36 | @end 37 | -------------------------------------------------------------------------------- /UIViewController+BackButtonHandler.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIViewController+BackButtonHandler.m 3 | // 4 | // Created by Sergey Nikitenko on 10/1/13. 5 | // Copyright 2013 Sergey Nikitenko. All rights reserved. 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in 15 | // all copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | // THE SOFTWARE. 24 | // 25 | 26 | #import "UIViewController+BackButtonHandler.h" 27 | #import 28 | 29 | @implementation UIViewController (BackButtonHandler) 30 | 31 | @end 32 | 33 | @implementation UINavigationController (ShouldPopOnBackButton) 34 | 35 | + (void)load { 36 | Method originalMethod = class_getInstanceMethod([self class], @selector(navigationBar:shouldPopItem:)); 37 | Method overloadingMethod = class_getInstanceMethod([self class], @selector(overloaded_navigationBar:shouldPopItem:)); 38 | method_setImplementation(originalMethod, method_getImplementation(overloadingMethod)); 39 | } 40 | 41 | - (BOOL)overloaded_navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item { 42 | 43 | if([self.viewControllers count] < [navigationBar.items count]) { 44 | return YES; 45 | } 46 | 47 | BOOL shouldPop = YES; 48 | UIViewController* vc = [self topViewController]; 49 | if([vc respondsToSelector:@selector(navigationShouldPopOnBackButton)]) { 50 | shouldPop = [vc navigationShouldPopOnBackButton]; 51 | } 52 | 53 | if(shouldPop) { 54 | dispatch_async(dispatch_get_main_queue(), ^{ 55 | [self popViewControllerAnimated:YES]; 56 | }); 57 | } else { 58 | // Workaround for iOS7.1. Thanks to @boliva - http://stackoverflow.com/posts/comments/34452906 59 | for(UIView *subview in [navigationBar subviews]) { 60 | if(0. < subview.alpha && subview.alpha < 1.) { 61 | [UIView animateWithDuration:.25 animations:^{ 62 | subview.alpha = 1.; 63 | }]; 64 | } 65 | } 66 | } 67 | 68 | return NO; 69 | } 70 | 71 | @end 72 | -------------------------------------------------------------------------------- /UIViewController-BackButtonHandler.podspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | # 3 | # Be sure to run `pod spec lint UIViewController+BackButtonHandler.podspec' to ensure this is a 4 | # valid spec and to remove all comments including this before submitting the spec. 5 | # 6 | # To learn more about Podspec attributes see http://docs.cocoapods.org/specification.html 7 | # To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/ 8 | # 9 | 10 | Pod::Spec.new do |s| 11 | s.name = "UIViewController+BackButtonHandler" 12 | s.version = "1.0.0" 13 | s.summary = "The extension allows to handle UINavigationViewController's 'Back' button action." 14 | s.homepage = "https://github.com/onegray/UIViewController-BackButtonHandler" 15 | s.author = "onegray" 16 | s.platform = :ios 17 | s.source = { 18 | :git => "https://github.com/onegray/UIViewController-BackButtonHandler.git", 19 | :tag => "#{s.version}" 20 | } 21 | s.source_files = "UIViewController+BackButtonHandler.{h,m}" 22 | end 23 | --------------------------------------------------------------------------------