Skip to content

Commit

Permalink
Import the custom navigation & tabbar controller.
Browse files Browse the repository at this point in the history
  • Loading branch information
xingheng committed Dec 19, 2016
1 parent 6dd251f commit b0c4b3c
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 2 deletions.
30 changes: 30 additions & 0 deletions DSBaseViewController/CustomNavigationController.h
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
151 changes: 151 additions & 0 deletions DSBaseViewController/CustomNavigationController.m
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
15 changes: 15 additions & 0 deletions DSBaseViewController/CustomTabBarController.h
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
50 changes: 50 additions & 0 deletions DSBaseViewController/CustomTabBarController.m
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
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ PODS:
- CocoaLumberjack/Core
- CocoaLumberjack/Extensions (3.0.0):
- CocoaLumberjack/Default
- DSBaseViewController (1.0.0)
- DSBaseViewController (1.1.0)

DEPENDENCIES:
- CocoaLumberjack
Expand All @@ -19,7 +19,7 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
CocoaLumberjack: c823149bccc5519a9447aeb433be7b1212a7d6a5
DSBaseViewController: 0eac575f370838052c37d5b473839d3ec0b661ba
DSBaseViewController: cca026ae3f6cdcb39d82a67cbf139a56da8d2e48

PODFILE CHECKSUM: 67d6202f47a06c9f74553941d56b9bf68e1c8201

Expand Down

0 comments on commit b0c4b3c

Please sign in to comment.