关闭

自定义可以侧滑返回的navigation

143人阅读 评论(0) 收藏 举报
分类:

#import "NDNavigationController.h"


@interface NDNavigationController ()<UINavigationControllerDelegate, UIGestureRecognizerDelegate>

@property(nonatomic, weak) UIViewController *currentShowVC;

@end


@implementation NDNavigationController


- (void)viewDidLoad {

    [super viewDidLoad];

   

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

   

}


- (instancetype)initWithRootViewController:(UIViewController *)rootViewController {


    

    if (self = [super initWithRootViewController:rootViewController]) {

        self.delegate = self;

        self.interactivePopGestureRecognizer.delegate = self;

        // 设置navigationBar的背景颜色,根据需要自己设置

        self.navigationBar.barTintColor = kTabBarColor;

        // 设置navigationBar是否透明,不透明的话会使可用界面原点下移(00)点为导航栏左下角下方的那个点

        self.navigationBar.translucent = NO;

        // 设置navigationBar是不是使用系统默认返回,默认为YES

        self.interactivePopGestureRecognizer.enabled = YES;

        // 设置navigationBar元素的背景颜色,不包括title

        self.navigationBar.tintColor = KCommentColor;

        // 设置navigationControllertitle的字体颜色

        NSDictionary * dict=[NSDictionary dictionaryWithObject:kMainBlackColor forKey:NSForegroundColorAttributeName];

        self.navigationBar.titleTextAttributes = dict;

      

        // 统一替换 back item 的图片

        UIImage * image = [UIImage imageNamed:@"com_navigatin_back_bt"];

        image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

        [self.navigationBar setBackIndicatorImage:image];

        [self.navigationBar setBackIndicatorTransitionMaskImage:image];

    }

    

    return self;

}


- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

    if (1 == navigationController.viewControllers.count) {

        self.currentShowVC = nil;

    } else {

        self.currentShowVC = viewController;

    }

}


- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {

    if (gestureRecognizer == self.interactivePopGestureRecognizer) {

        return (self.currentShowVC == self.topViewController);

    }

    return YES;

}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] &&

        [otherGestureRecognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]) {

        return YES;

    } else {

        return NO;

    }

}

//解决:手指在滑动的时候,被 pop ViewController 中的 UIScrollView 会跟着一起滚动

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

    return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];

}

@end


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:24278次
    • 积分:912
    • 等级:
    • 排名:千里之外
    • 原创:62篇
    • 转载:53篇
    • 译文:0篇
    • 评论:0条