Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions SwipeBack.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ Pod::Spec.new do |s|
s.platform = :ios, '7.0'
s.requires_arc = true
s.source_files = 'SwipeBack/*.{h,m}'
s.frameworks = 'UIKit', 'Foundation', 'QuartzCore'
s.dependency 'JRSwizzle', '~> 1.0'
s.frameworks = 'UIKit', 'Foundation'
end
2 changes: 2 additions & 0 deletions SwipeBack/UINavigationController+SwipeBack.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#import <UIKit/UIKit.h>

void __swipeback_swizzle(Class cls, SEL originalSelector);

@interface UINavigationController (SwipeBack) <UIGestureRecognizerDelegate>

@property (nonatomic, assign) BOOL swipeBackEnabled; // default is `YES`
Expand Down
42 changes: 29 additions & 13 deletions SwipeBack/UINavigationController+SwipeBack.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,53 @@
// SOFTWARE.
//


#import <JRSwizzle/JRSwizzle.h>
#import <objc/runtime.h>

#import "UINavigationController+SwipeBack.h"


void __swipeback_swizzle(Class cls, SEL originalSelector) {
NSString *originalName = NSStringFromSelector(originalSelector);
NSString *alternativeName = [NSString stringWithFormat:@"swizzled_%@", originalName];

SEL alternativeSelector = NSSelectorFromString(alternativeName);

Method originalMethod = class_getInstanceMethod(cls, originalSelector);
Method alternativeMethod = class_getInstanceMethod(cls, alternativeSelector);

class_addMethod(cls,
originalSelector,
class_getMethodImplementation(cls, originalSelector),
method_getTypeEncoding(originalMethod));
class_addMethod(cls,
alternativeSelector,
class_getMethodImplementation(cls, alternativeSelector),
method_getTypeEncoding(alternativeMethod));

method_exchangeImplementations(class_getInstanceMethod(cls, originalSelector),
class_getInstanceMethod(cls, alternativeSelector));
}


@implementation UINavigationController (SwipeBack)

+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[self jr_swizzleMethod:@selector(viewDidLoad)
withMethod:@selector(hack_viewDidLoad)
error:nil];
[self jr_swizzleMethod:@selector(pushViewController:animated:)
withMethod:@selector(hack_pushViewController:animated:)
error:nil];
__swipeback_swizzle(self, @selector(viewDidLoad));
__swipeback_swizzle(self, @selector(pushViewController:animated:));
});
}

- (void)hack_viewDidLoad
- (void)swizzled_viewDidLoad
{
[self hack_viewDidLoad];
[self swizzled_viewDidLoad];
self.interactivePopGestureRecognizer.delegate = self.swipeBackEnabled ? self : nil;
}

- (void)hack_pushViewController:(UIViewController *)viewController animated:(BOOL)animated
- (void)swizzled_pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[self hack_pushViewController:viewController animated:animated];
[self swizzled_pushViewController:viewController animated:animated];
self.interactivePopGestureRecognizer.enabled = NO;
}

Expand Down
22 changes: 13 additions & 9 deletions SwipeBack/UIViewController+SwipeBack.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,32 @@
// SOFTWARE.
//


#import <JRSwizzle/JRSwizzle.h>

#import <objc/runtime.h>
#import "UINavigationController+SwipeBack.h"
#import "UIViewController+SwipeBack.h"


@implementation UIViewController (SwipeBack)

+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[self jr_swizzleMethod:@selector(viewDidAppear:)
withMethod:@selector(hack_viewDidAppear:)
error:nil];
__swipeback_swizzle(self, @selector(viewDidAppear:));
// [self swizzle:@selector(viewDidAppear:)];
});
}

- (void)hack_viewDidAppear:(BOOL)animated
+ (void)swizzle:(SEL)selector
{
NSString *name = [NSString stringWithFormat:@"swizzled_%@", NSStringFromSelector(selector)];
Method m1 = class_getInstanceMethod(self, selector);
Method m2 = class_getInstanceMethod(self, NSSelectorFromString(name));
method_exchangeImplementations(m1, m2);
}

- (void)swizzled_viewDidAppear:(BOOL)animated
{
[self hack_viewDidAppear:animated];
[self swizzled_viewDidAppear:animated];
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}

Expand Down