Solutions to IOS left slide gesture failure


After ios7, apple optimized a small function, that is, for the uiviewcontroller in the uinavagationcontroller stack, as long as you gently slide right on the left edge of the view controller, the view controller will pop out of the stack (provided, of course, for non root view controllers). The implementation method is very simple. It can be done in one sentence:

self.navigationController.interactivePopGestureRecognizer.enabled = YES;

In fact, for a view controller, the default value of this attribute is yes, so the right slide pop function can be realized without setting.

However, this function is very limited because it does not allow the current view controller to customize the leftbarbuttonitem. Once customized, the right slide function will fail. Here’s a way:

self.navigationController.interactivePopGestureRecognizer.delegate = nil;

After setting the proxy to nil, you can right slide pop even if you customize the leftbarbuttonitem.

Alternatively, turn on the permission of the gesture:

self.navigationController.interactivePopGestureRecognizer.enabled = YES ;

In fact, if the leftbarbuttonitem is customized, the common practice is to reset the proxy:

- (void)viewDidAppear:(BOOL)animated{
   self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;

Then implement gesture protocol:

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer{
  //Judge whether it is rootviewcontroller
  if (self.navigationController && self.navigationController.viewControllers.count == 1) {
    return NO;
  return YES;

However, if our custom return button is only text or picture, this setting can be set without invalidation

  UIBarButtonItem *item = [[UIBarButtonItem alloc]init];
  item.title = @"";
  self.navigationItem.backBarButtonItem = item;

If you want to customize the view as a button, you need to use the leftbarbuttonitem setting and the above scheme to prevent gesture failure

A friend suggested that the above methods will cause the interface to fake death after sliding for many times. Here is another solution:

Add the following code to the viewdidappear and viewwilldisappear of all pages except level 1 Pages:

- (void)viewWillDisappear:(BOOL)animated {
  [super viewWillDisappear:animated];
  //Leave the agent blank, otherwise it will flash back
  if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;
- (void)viewDidAppear:(BOOL)animated { 
  [super viewDidAppear:animated]; 
  //Turn on the sliding return effect of ios7 
  if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) { 
    //Effective only on secondary pages 
    if ([self.navigationController.viewControllers count] == 2) { 
      self.navigationController.interactivePopGestureRecognizer.delegate = self; 

Implement the following methods in delegate of UINavigationController:

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated { 
//Turn on sliding gesture
  if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
    navigationController.interactivePopGestureRecognizer.enabled = YES;

Add the following code before pushviewcontroller:

//In the process of interface switching, sliding gesture is prohibited to avoid interface jamming
if ([_currentNav respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
  _currentNav.interactivePopGestureRecognizer.enabled = NO;
[_currentNav pushViewController:viewController animated:YES];

The sliding return can be realized while avoiding the problem of interface jamming.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.

Recommended Today

SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress: recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]