-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Import the custom navigation & tabbar controller.
- Loading branch information
Showing
5 changed files
with
248 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// CustomNavigationController.h | ||
// youyue | ||
// | ||
// Created by WeiHan on 12/3/15. | ||
// Copyright © 2015 DragonSource. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
#import "DSBaseViewController.h" | ||
|
||
@interface CustomNavigationController : UINavigationController | ||
|
||
@property (nonatomic, assign) BOOL enableInteractivePopGesture; // default is YES | ||
|
||
/** | ||
* @brief Pops view controllers until the specified view controller is at the top of the navigation stack. (UINavigationController -popToViewController:animated:) | ||
* | ||
* @return Return the top view controller after pop. | ||
*/ | ||
- (UIViewController *)popToViewControllerClass:(Class)viewControllerClass animated:(BOOL)animated; | ||
|
||
@end | ||
|
||
@interface DSBaseViewController (Navigation) | ||
|
||
- (UIViewController *)popToViewController:(Class)viewControllerClass; // with animation | ||
- (UIViewController *)popToViewController:(Class)viewControllerClass animated:(BOOL)animated; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
// | ||
// CustomNavigationController.m | ||
// youyue | ||
// | ||
// Created by WeiHan on 12/3/15. | ||
// Copyright © 2015 DragonSource. All rights reserved. | ||
// | ||
// Solution fix for custom left navigation bar disabled back swipe gesture: | ||
// http://keighl.com/post/ios7-interactive-pop-gesture-custom-back-button/ | ||
// http://stackoverflow.com/questions/19054625/changing-back-button-in-ios-7-disables-swipe-to-navigate-back/20330647#20330647 | ||
// http://www.cnblogs.com/angzn/p/3696901.html | ||
// References in future: | ||
// https://github.com/fastred/AHKNavigationController | ||
// | ||
|
||
#import "CustomNavigationController.h" | ||
|
||
#define InteractivePopGesture 1 // hack! | ||
|
||
#pragma mark - CustomNavigationController | ||
|
||
@interface CustomNavigationController () <UINavigationControllerDelegate, UIGestureRecognizerDelegate> | ||
|
||
@end | ||
|
||
@implementation CustomNavigationController | ||
|
||
- (instancetype)initWithRootViewController:(UIViewController *)rootViewController | ||
{ | ||
self.enableInteractivePopGesture = YES; | ||
|
||
if (self = [super initWithRootViewController:rootViewController]) { | ||
// make the content views' vertical offset in view controller starts from navigation bar's bottom instead of top screen. | ||
self.navigationBar.translucent = NO; | ||
self.delegate = self; | ||
} | ||
|
||
return self; | ||
} | ||
|
||
- (void)viewDidLoad | ||
{ | ||
[super viewDidLoad]; | ||
|
||
#if InteractivePopGesture | ||
|
||
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) { | ||
self.interactivePopGestureRecognizer.delegate = self; | ||
} | ||
|
||
#endif | ||
} | ||
|
||
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated | ||
{ | ||
#if InteractivePopGesture | ||
[self setInteractivePopGestureState:NO]; | ||
#endif | ||
|
||
if (self.viewControllers.count >= 1) { | ||
viewController.hidesBottomBarWhenPushed = YES; | ||
} | ||
|
||
[super pushViewController:viewController animated:animated]; | ||
} | ||
|
||
- (void)didReceiveMemoryWarning | ||
{ | ||
[super didReceiveMemoryWarning]; | ||
} | ||
|
||
- (void)dealloc | ||
{ | ||
self.interactivePopGestureRecognizer.delegate = nil; | ||
self.delegate = nil; | ||
} | ||
|
||
#pragma mark - Public | ||
|
||
- (UIViewController *)popToViewControllerClass:(Class)viewControllerClass animated:(BOOL)animated | ||
{ | ||
NSParameterAssert(viewControllerClass && [viewControllerClass isSubclassOfClass:[UIViewController class]]); | ||
|
||
NSArray<UIViewController *> *vcs = self.viewControllers; | ||
__block UIViewController *resultVC = nil; | ||
|
||
[vcs enumerateObjectsUsingBlock:^(UIViewController *viewController, NSUInteger idx, BOOL *stop) { | ||
if ([viewController isKindOfClass:viewControllerClass]) { | ||
resultVC = viewController; | ||
*stop = YES; | ||
} | ||
}]; | ||
|
||
if (resultVC) { | ||
[super popToViewController:resultVC animated:animated]; | ||
} | ||
|
||
return resultVC; | ||
} | ||
|
||
#pragma mark - Private | ||
|
||
#if InteractivePopGesture | ||
|
||
- (void)setInteractivePopGestureState:(BOOL)state | ||
{ | ||
if (!self.enableInteractivePopGesture) { | ||
state = NO; | ||
} | ||
|
||
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) { | ||
self.interactivePopGestureRecognizer.enabled = state; | ||
} | ||
} | ||
|
||
#endif | ||
|
||
#pragma mark - UINavigationControllerDelegate | ||
|
||
- (void)navigationController:(UINavigationController *)navigationController | ||
didShowViewController:(UIViewController *)viewController | ||
animated:(BOOL)animate | ||
{ | ||
#if InteractivePopGesture | ||
// Enable the gesture again once the new controller is shown except for root view controller. | ||
[self setInteractivePopGestureState:navigationController.viewControllers.count > 1]; | ||
#endif | ||
} | ||
|
||
@end | ||
|
||
|
||
#pragma mark - DSBaseViewController (Navigation) | ||
|
||
@implementation DSBaseViewController (Navigation) | ||
|
||
- (UIViewController *)popToViewController:(Class)viewControllerClass | ||
{ | ||
return [self popToViewController:viewControllerClass animated:YES]; | ||
} | ||
|
||
- (UIViewController *)popToViewController:(Class)viewControllerClass animated:(BOOL)animated | ||
{ | ||
NSParameterAssert(viewControllerClass && [viewControllerClass isSubclassOfClass:[DSBaseViewController class]]); | ||
|
||
CustomNavigationController *naviVC = (CustomNavigationController *)self.navigationController; | ||
|
||
return [naviVC popToViewControllerClass:viewControllerClass animated:animated]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// CustomTabBarController.h | ||
// DragonSourceCommon | ||
// | ||
// Created by WeiHan on 12/23/15. | ||
// Copyright © 2015 DragonSource. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
@interface CustomTabBarController : UITabBarController | ||
|
||
@property (nonatomic, copy) BOOL (^ shouldSelectViewControllerBlock)(NSInteger index, UIViewController *viewController); | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// CustomTabBarController.m | ||
// DragonSourceCommon | ||
// | ||
// Created by WeiHan on 12/23/15. | ||
// Copyright © 2015 DragonSource. All rights reserved. | ||
// | ||
|
||
#import "CustomTabBarController.h" | ||
|
||
@interface CustomTabBarController ()<UITabBarControllerDelegate> | ||
|
||
@end | ||
|
||
@implementation CustomTabBarController | ||
|
||
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil | ||
{ | ||
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { | ||
self.tabBar.translucent = NO; | ||
self.delegate = self; | ||
} | ||
|
||
return self; | ||
} | ||
|
||
- (void)viewDidLoad | ||
{ | ||
[super viewDidLoad]; | ||
// Do any additional setup after loading the view. | ||
} | ||
|
||
- (void)didReceiveMemoryWarning | ||
{ | ||
[super didReceiveMemoryWarning]; | ||
} | ||
|
||
#pragma mark - UITabBarControllerDelegate | ||
|
||
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController | ||
{ | ||
if (self.shouldSelectViewControllerBlock) { | ||
NSInteger index = [tabBarController.viewControllers indexOfObject:viewController]; | ||
return self.shouldSelectViewControllerBlock(index, viewController); | ||
} | ||
|
||
return YES; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters