自定义的导航控制器(UINavigationController),并且添加一个任意位置可以返回的丝滑手势

// EFNavigationController.h
// JueyingWeibo
//
// Created by Jueying on 15/3/4.
// Copyright (c) 2015年 MyCompany. All rights reserved.
//

     @interface EFNavigationController : UINavigationController

     @end

// EFNavigationController.m
// JueyingWeibo
//
// Created by Jueying on 15/3/4.
// Copyright (c) 2015年 MyCompany. All rights reserved.
//

    #import "EFNavigationController.h"
     @interface EFNavigationController ()
    
    @end

    @implementation EFNavigationController

当EFNavigationController这个类第一次加载的时候,只调用一次
+initialize,这个方法是当第一次给这个类发送消息时调用一次

    + (void)initialize {
    // 设置导航控制器barButtonItem的title颜色
    [[UIBarButtonItem appearance]
     setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor 
     orangeColor]} forState:UIControlStateNormal];

    [[UIBarButtonItem appearance] 
    setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor
     redColor]} forState:UIControlStateHighlighted];

    [[UIBarButtonItem appearance] 
    setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor 
    lightGrayColor]} forState:UIControlStateDisabled];

    }

    - (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self leftTouchBack];
    }

    - (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

    }

#pragma mark - Pan gestureRecognizer

- (void)leftTouchBack {
    // 获取系统自带滑动手势的target对象
    id target = self.interactivePopGestureRecognizer.delegate;

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
#pragma clang diagnostic pop
    pan.delegate = self;
    [self.view addGestureRecognizer:pan];
    self.interactivePopGestureRecognizer.enabled = NO;
    _panGesture = pan;  
// 创建一个成员变量,可以在禁止手势的地方直接调用_panGesture.enable = NO;来禁用手势。
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if (self.childViewControllers.count == 1) {
        return NO;
    }
    return YES;
}
  
#pragma mark - 重写push方法

    - (void)pushViewController:(UIViewController *)viewController
    animated:(BOOL)animated {

     if (self.viewControllers.count > 0) {//除了根视图控制器以外的所有控
    制,以压栈的方式push进来,就隐藏tabbar

    viewController.hidesBottomBarWhenPushed = YES;

     viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem 
     itemWithNormal:@"navigationbar_back" 
     highlighted:@"navigationbar_back_highlighted" target:self
    action:@selector(back)];

    viewController.navigationItem.rightBarButtonItem = [UIBarButtonItem 
    itemWithNormal:@"navigationbar_more" 
    highlighted:@"navigationbar_more_highlighted" target:self 
    action:@selector(popToRoot)];

    }
     [super pushViewController:viewController animated:animated];
    }

    - (void)back {

    [self popViewControllerAnimated:YES];

    }

    - (void)popToRoot {

    [self popToRootViewControllerAnimated:YES];

    }
    @end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,482评论 0 23
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 10,531评论 5 13
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 12,719评论 6 30
  • 我们偶尔会嘲笑有梦想的人,经常或真或假地说理想不是用来实现的。很多人很努力,但大部分已经无形“放纵”了自己,反正都...
    苏夏阅读 3,269评论 0 5
  • “成功的花儿,人们只惊羡它现时的美丽。当初它的芽儿浸透了奋斗的泪水,洒遍了牺牲的血雨。”——《繁星·春水》 想要获...
    赫拉克勒斯丶穆阅读 3,921评论 1 2