Skip to content
This repository was archived by the owner on Dec 24, 2019. It is now read-only.
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@
@interface JTNavigationController : UINavigationController

@property (nonatomic, strong) UIImage *backButtonImage;

@property (nonatomic, assign) BOOL fullScreenPopGestureEnabled;

@property (nonatomic, copy, readonly) NSArray *jt_viewControllers;

@end


@interface UINavigationController (JTExtention)

@property (nonatomic, readonly) UINavigationControllerOperation jt_operation;

- (NSArray *)jt_popViewControllerTwiceAnimated:(BOOL)animated;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@

#define kDefaultBackImageName @"backImage"

@interface JTNavigationController () <UINavigationControllerDelegate, UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIPanGestureRecognizer *popPanGesture;
@property (nonatomic, strong) id popGestureDelegate;
@property (nonatomic, assign) UINavigationControllerOperation jt_operation;

@end

#pragma mark - JTWrapNavigationController

@interface JTWrapNavigationController : UINavigationController
Expand All @@ -20,42 +28,48 @@ @interface JTWrapNavigationController : UINavigationController
@implementation JTWrapNavigationController

- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
self.jt_navigationController.jt_operation = UINavigationControllerOperationPop;
return [self.navigationController popViewControllerAnimated:animated];
}

- (NSArray<UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated {
self.jt_navigationController.jt_operation = UINavigationControllerOperationPop;
return [self.navigationController popToRootViewControllerAnimated:animated];
}

- (NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
JTNavigationController *jt_navigationController = viewController.jt_navigationController;
jt_navigationController.jt_operation = UINavigationControllerOperationPop;
NSInteger index = [jt_navigationController.jt_viewControllers indexOfObject:viewController];
return [self.navigationController popToViewController:jt_navigationController.viewControllers[index] animated:animated];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
if (![self.navigationController isKindOfClass:[JTNavigationController class]]) {
return;
}

viewController.jt_navigationController = (JTNavigationController *)self.navigationController;
viewController.jt_fullScreenPopGestureEnabled = viewController.jt_navigationController.fullScreenPopGestureEnabled;
viewController.jt_navigationController.jt_operation = UINavigationControllerOperationPush;

UIImage *backButtonImage = viewController.jt_navigationController.backButtonImage;

if (!backButtonImage) {
backButtonImage = [UIImage imageNamed:kDefaultBackImageName];
}

viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:backButtonImage style:UIBarButtonItemStylePlain target:self action:@selector(didTapBackButton)];
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:backButtonImage
style:UIBarButtonItemStylePlain
target:viewController
action:@selector(jt_didTapBackButton:)];

[self.navigationController pushViewController:[JTWrapViewController wrapViewControllerWithViewController:viewController] animated:animated];
}

- (void)didTapBackButton {
[self.navigationController popViewControllerAnimated:YES];
JTWrapViewController* wrapController = [JTWrapViewController wrapViewControllerWithViewController:viewController];
[self.navigationController pushViewController:wrapController animated:animated];
}

-(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion{
[self.navigationController dismissViewControllerAnimated:flag completion:completion];
self.viewControllers.firstObject.jt_navigationController=nil;
self.viewControllers.firstObject.jt_navigationController = nil;
}

@end
Expand All @@ -67,6 +81,10 @@ -(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))compl
@implementation JTWrapViewController

+ (JTWrapViewController *)wrapViewControllerWithViewController:(UIViewController *)viewController {
if (!viewController) {
NSLog(@"%s %d Warning: wrapViewControllerWithViewController:nil", __FUNCTION__, __LINE__);
return nil;
}

JTWrapNavigationController *wrapNavController = [[JTWrapNavigationController alloc] init];
wrapNavController.viewControllers = @[viewController];
Expand Down Expand Up @@ -136,15 +154,9 @@ - (UIViewController *)rootViewController {

#pragma mark - JTNavigationController

@interface JTNavigationController () <UINavigationControllerDelegate, UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIPanGestureRecognizer *popPanGesture;

@property (nonatomic, strong) id popGestureDelegate;

@end

@implementation JTNavigationController
@implementation JTNavigationController {
BOOL _shouldNextGestureFailed;
}

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController {
if (self = [super init]) {
Expand Down Expand Up @@ -172,9 +184,43 @@ - (void)viewDidLoad {
SEL action = NSSelectorFromString(@"handleNavigationTransition:");
self.popPanGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self.popGestureDelegate action:action];
self.popPanGesture.maximumNumberOfTouches = 1;
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
UIViewController* result = [super popViewControllerAnimated:animated];
self.jt_operation = UINavigationControllerOperationPop;

return result;
}

#pragma mark - 横屏控制支持

- (BOOL)shouldAutorotate {
if (self.jt_viewControllers.count > 0) {
UIViewController* root = [self.jt_viewControllers firstObject];
return [root shouldAutorotate];
}

return [super shouldAutorotate];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
if (self.jt_viewControllers.count > 0) {
UIViewController* root = [self.jt_viewControllers firstObject];
return [root supportedInterfaceOrientations];
}

return [super supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
if (self.jt_viewControllers.count > 0) {
UIViewController* root = [self.jt_viewControllers firstObject];
return [root preferredInterfaceOrientationForPresentation];
}

return [super preferredInterfaceOrientationForPresentation];
}

#pragma mark - UINavigationControllerDelegate

Expand Down Expand Up @@ -220,3 +266,37 @@ - (NSArray *)jt_viewControllers {
}

@end


@implementation UINavigationController (JTExtention)

- (UINavigationControllerOperation)jt_operation {
if ([self isKindOfClass:[JTNavigationController class]]) {
JTNavigationController* controller = (JTNavigationController *)self;
return controller.jt_operation;
}

return UINavigationControllerOperationNone;
}

- (NSArray *)jt_popViewControllerTwiceAnimated:(BOOL)animated {
if (![self.navigationController isKindOfClass:[JTNavigationController class]]) {
return nil;
}

JTNavigationController* jt_navigationController = (JTNavigationController *)self.navigationController;
NSArray* viewControllers = jt_navigationController.jt_viewControllers;
NSInteger count = viewControllers.count;
NSInteger index = count - 3;
if (index < 0) {
// 出错了,没办法pop两次
UIViewController* controller = [self popViewControllerAnimated:animated];
NSArray* controllers = @[controller];
return controllers;
}

UIViewController* viewController = [viewControllers objectAtIndex:index];
return [self popToViewController:viewController animated:animated];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
@interface UIViewController (JTNavigationExtension)

@property (nonatomic, assign) BOOL jt_fullScreenPopGestureEnabled;

@property (nonatomic, strong) JTNavigationController *jt_navigationController;

- (void)jt_didTapBackButton:(id)sender;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "UIViewController+JTNavigationExtension.h"
#import "JTNavigationController.h"
#import <objc/runtime.h>

@implementation UIViewController (JTNavigationExtension)
Expand All @@ -24,7 +25,11 @@ - (JTNavigationController *)jt_navigationController {
}

- (void)setJt_navigationController:(JTNavigationController *)navigationController {
objc_setAssociatedObject(self, @selector(jt_navigationController), navigationController, OBJC_ASSOCIATION_RETAIN);
objc_setAssociatedObject(self, @selector(jt_navigationController), navigationController, OBJC_ASSOCIATION_ASSIGN);
}

- (void)jt_didTapBackButton:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}

@end