Basil Bourque
Basil Bourque

Reputation: 338316

New gesture - Swipe left to right - as shortcut for Back button in UINavigationController in old app

iOS 7 sports a new gesture, swiping left to right across the screen as a shortcut for the Back button in a UINavigationController.

My app does not seem to be picking up this behavior for free. What do I need to do to make this gesture available to my iOS app (built for iOS 5.1 and later in Xcode 4.6.3)?

Here's an article with video, The new gesture in iOS 7 you want to know about, from a user's perspective.

The answer may have something to do with interactivePopGestureRecognizer which is a UIGestureRecognizer subclass.

Upvotes: 5

Views: 11423

Answers (2)

Waqas Haider Sheikh
Waqas Haider Sheikh

Reputation: 870

I have found the perfect solution for custom leftBarButtom problem

Set the gesture delegate to the navigation controller

you need to subclass UINavigationController and implement "UIGestureRecognizerDelegate" protocol in it, and add some code on viewDidLoad see the following code below

CustomNavigationController.m

@interface CBNavigationController : UINavigationController @end

@implementation CBNavigationController

  • (void)viewDidLoad { __weak CBNavigationController *weakSelf = self;

    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) { self.interactivePopGestureRecognizer.delegate = weakSelf; } }

@end

and just inherit this class to your Navigation controller, it will work perfect.

Upvotes: 0

Greg
Greg

Reputation: 33650

It should work automatically if the back button is visible. If you are displaying a leftBarButtonItem instead of the back button, the gesture will not be present by default. Also, if you are using a UINavigationBar but not a UINavigationController, you won't see this functionality.

If you are using a UINavigationController and your view controller's navigation item contains a leftBarButtonItem, it's still possible to add functionality for the swipe left to right gesture of the navigation controller, by attaching a delegate to the navigation controller's interactivePopGestureRecognizer.

EDIT: I didn't notice that you're building against SDK 5.1. This is a new feature in the iOS 7 SDK, so I believe you'll need to build with Xcode 5 against the iOS 7 SDK in order to make use of the new feature.

Upvotes: 4

Related Questions