统一设置navigation返回和左划手势返回

首先继承UINavigationController的类里面进行书写


///重写push方法 push的控制器隐藏tabbar

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

{

if (self.viewControllers.count > 0) {

viewController.hidesBottomBarWhenPushed = YES;

//1.添加后退按钮

[self addBackButton:viewController];

}

[super pushViewController:viewController animated:animated];

}

//2 自定义后退按钮

- (void)addBackButton:(UIViewController *)viewController {

//开启手势后退

self.interactivePopGestureRecognizer.delegate = (id)self;

//开启手势滑动后退

if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {

self.interactivePopGestureRecognizer.enabled = YES;

}

UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:(UIBarButtonItemStyleDone) target:self action:@selector(backClick)];

//间距

UIBarButtonItem *fixed = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];

fixed.width = -10;

viewController.navigationItem.leftBarButtonItems =@[fixed,back];

}

//点击后退的时候执行的方法

- (void)backClick {

[self popViewControllerAnimated:YES];

}

你可能感兴趣的:(统一设置navigation返回和左划手势返回)