自定义NavigationController 实现侧滑 返回

1.自定义标题

2.自定义返回按钮

3.自定义全屏侧滑手势

4.重写push方法实现隐藏底部tabbar

+ (void)load
{
    // 获取当前类下的导航条
    UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedIn:self, nil];
    
    // bug:
    // iOS7,iOS8 bug:把短信界面导航条改了,联系人界面会出现黑
    
    // 设置标题字体
    // 设置导航条标题字体 => 拿到导航条去设置
    [UINavigationBar appearance];
    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
    attr[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20];
    
    navBar.titleTextAttributes = attr;
    
    //可以在此处设置导航条背景图片
    [navBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //自定义导航条标题
   self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont boldSystemFontOfSize:20]};
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    //也可在此处设置导航颜色(切记颜色尺寸高度为64,不然会电池状态栏颜色变黑)
    UIImage *backImage = [UIImage imageNamed:@"backImage"];
    [self.navigationController.navigationBar setBackgroundImage:backImage forBarMetrics:UIBarMetricsDefault];
     [self setNeedsStatusBarAppearanceUpdate];

    // 滑动功能,自定义手势触发系统侧滑返回(使用API私有的 handleNavigationTransition:)
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
    [self.view addGestureRecognizer:pan];
    
//    // 控制器手势什么时候触发
    pan.delegate = self;

    //关闭系统侧滑返回
    self.interactivePopGestureRecognizer.enabled = NO;
    
    // 清空手势代理,恢复滑动返回功能(使用默认的侧边返回手势)
//    self.interactivePopGestureRecognizer.delegate = self;
}

//重写ViewController的状态栏颜色方法(注:如果在info.plist里把viewController_based status 设为YES的话(默认是YES)需要此方法改变statusBar颜色)
- (UIStatusBarStyle)preferredStatusBarStyle
{
    
    return UIStatusBarStyleLightContent;
    
}


#pragma mark - UIGestureRecognizerDelegate
// 是否触发手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    // 在根控制器下 不要 触发手势(否则在根控制器下侧滑会导致崩溃)
    return self.childViewControllers.count > 1;
}


- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.childViewControllers.count > 0) { // 非根控制器
        
        viewController.hidesBottomBarWhenPushed = YES;
        
        // 非根控制器才需要设置返回按钮
        // 设置返回按钮
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [backButton setTitle:@"返回" forState:UIControlStateNormal];
        [backButton setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
        [backButton setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
        [backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [backButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
        [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [backButton sizeToFit];
        
        // 注意:一定要在按钮内容有尺寸的时候,设置才有效果
        backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -25, 0, 0);
        
        // 设置返回按钮
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
        
    }
    
    // 这个方法才是真正执行跳转
    [super pushViewController:viewController animated:animated];
}

- (void)back
{
    [self popViewControllerAnimated:YES];
}

你可能感兴趣的:(自定义NavigationController 实现侧滑 返回)