UINavigationController Interactive Pop Gesture cancellation Not Working in iOS 8? - ios

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return NO;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if ([self.navigationController respondsToSelector:#selector(interactivePopGestureRecognizer)])
{
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if ([self.navigationController respondsToSelector:#selector(interactivePopGestureRecognizer)])
{
[self.navigationController.interactivePopGestureRecognizer setEnabled:YES];
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
}
//Am using this code but it not working in iOS 8.3 .Is there any problem in storyboard or it is not supported in iOS 8. Am irritating please give me a solution.

Create custom UIBarButtonItem and then set it in your navigation controller
self.navigationItem.leftBarButtonItem = customBarItem;
it might be helpful for you.

Add this 2 lines in the view controller's viewDidLoad method where you want to disable pop gesture
[self.navigationController.interactivePopGestureRecognizer setEnabled:NO];
self.navigationController.interactivePopGestureRecognizer.delegate = self;

Related

UILongPressGestureRecognizer not working after archiving application

When testing in iPad, I can successfully call the method attached to UILongPressGestureRecognizer in UILabel.
After I have archived the application for Enterprise deployment, UILongPressGestureRecognizer does not work anymore.
I have already added the following codes aside from manually ticking User Interaction Enabled:
In .h file:
#interface BGMSetMenuViewController : UIViewController <UIGestureRecognizerDelegate>
In .m file:
- (void)viewDidLoad
{
[super viewDidLoad];
itemPriceLabel.userInteractionEnabled = YES;
itemNameLabel.userInteractionEnabled = YES;
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressSetMenuPrice:)];
longPress.delegate = self;
[itemPriceLabel addGestureRecognizer:longPress];
}
#pragma mark - UILongPressGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
- (void)longPressSetMenuPrice:(UILongPressGestureRecognizer*)gesture
{
if (gesture.state == UIGestureRecognizerStateEnded) {
BGMPasscodeViewController *passcodeVC = [[BGMPasscodeViewController alloc] initWithNibName:#"BGMPasscodeViewController" bundle:nil];
self.providesPresentationContextTransitionStyle = YES;
self.definesPresentationContext = YES;
[passcodeVC setModalPresentationStyle:UIModalPresentationOverCurrentContext];
[self presentViewController:passcodeVC animated:YES completion:nil];
}
}
Anybody here who have the same experience?
Please note that this was working in Xcode 7 and iOS 9.
Now, I'm using Xcode 8 and testing in iOS 10.0.2 and it doesn't work anymore.
Try below code:set YES to label userIntractionEnabled also in storyboard
- (void)viewDidLoad {
[super viewDidLoad];
labelTouch.userInteractionEnabled = YES;
[self labelLongPressed:labelTouch];}
- (void)labelLongPressed:(UILabel *)label{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(handleLongPress:)];
longPress.minimumPressDuration = 2; //seconds
longPress.delegate = self;
[label addGestureRecognizer:longPress];}
-(void) handleLongPress : (UITapGestureRecognizer *)sender{
UIButton *button = (UIButton *)sender.view;
NSLog(#"longpress");}

iOS Push to another view controller get stuck

I have an base view controller and all my viewcontrollers inherit it.
#interface BaseViewController () <UIGestureRecognizerDelegate>
#end
#implementation BaseViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.tabBarController.tabBar.translucent = NO;
self.navigationController.navigationBar.translucent = NO;
self.navigationController.interactivePopGestureRecognizer.delegate = self;
self.view.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.backButton];
self.navigationItem.backBarButtonItem.title = #"";
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleDefault;
}
- (void) popToPreViewController {
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - getter and setter
- (UIButton *) backButton {
if (!_backButton) {
_backButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 20.0, 20.0)];
[_backButton addTarget:self action:#selector(popToPreViewController) forControlEvents:UIControlEventTouchUpInside];
[_backButton setImage:[UIImage imageNamed:#"main_back"] forState:UIControlStateNormal];
_backButton.hidden = YES;
}
return _backButton;
}
#end
Sometimes push to another view controller will get stuck ,but the app does not crash.Press home button and open the app again, it shows the another view controller . Is there something wrong with this baseviewcontroller ?
I have been troubled by this problem for a long time, now find a solution.
The reason for this problem is that the root controller performs a gesture pop operation, the problem described above occurs. The workaround is to disable the gesture pop at the root controller and open the gesture pop on the non root controller.
/// The controller is fully displayed, opening the gesture pop
Func navigationController (_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
If viewControllers.first == viewController {
InteractivePopGestureRecognizer? .isEnabled = false
} Else {
InteractivePopGestureRecognizer? .isEnabled = true
}
NavigationBar.sendSubview (toBack: navigationBar.overlay)
}

Disable scrolling when interactivePopGestureRecognizer transition is taking place

I have implemented the interactivePopGestureRecognizer. It transitions to the previous page. But the problem is that when the transition takes place, if there is a UIScrollView in the current view controller, it starts scrolling. Is there some way to prevent it?
I have added the gesture in my RootViewcontroller:
self.appNavController = [[UINavigationController alloc] initWithRootViewController:controller];
self.appNavController.interactivePopGestureRecognizer.enabled = YES;
self.appNavController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
[self.appNavController setNavigationBarHidden:YES];
I call this in:
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
APP_DELEGATE.rootViewController.appNavController.interactivePopGestureRecognizer.enabled = NO;
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
APP_DELEGATE.rootViewController.appNavController.interactivePopGestureRecognizer.enabled = YES;
}
I found the solution. In the view controller which is being swiped i added the following:
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
self.scrollView.scrollEnabled = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
self.scrollView.scrollEnabled = NO;
}
It worked like a charm.

self.navigationItem.hidesBackButton = YES and go back with swipe gesture on iOS7

I write the codes below in my UIViewController that uses UINavigationController.
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.hidesBackButton = YES;
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>) self;
}
I build and run my app,
self.navigationItem.hidesBackButton = YES;
above that works correctly, but
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>) self;
that one DO NOT works.
So, I re-write the code below.
- (void)viewDidLoad {
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 44.0f, 44.0f)]];
backBarButton.tintColor = [UIColor clearColor];
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
self.navigationItem.leftBarButtonItem = backBarButton;
}
It works correctly.
However, I want to use the first example.
The first one clearly express what I want to do.
Does someone have any idea?
In viewDidLoad, the view controller is not yet contained in a navigation controller, so the navigationController property is nil, which is why that line has no effect.
That said, assigning the delegate of UINavigationController's interactivePopGestureRecognizer is not good practice (I'm pretty sure it expects to be assigned to the navigation controller). Try disabling the gesture recognizer in viewWillAppear: instead:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

Handle several Tap events

I have an UIView which has an UITapGestureRecognizer attached to it that I use to hide the keyboard when the user taps outside the UITextFields. Now, I also have some labels that when tapped show an UIPickerView. The labels use an UITapGestureRecognizer as well. The problem is that the events seem to canibalise themselves.
Is it possible to execute both event handlers when tapping on my labels?
Thank you.
UITapGestureRecognizer* tapForUnit = [[UITapGestureRecognizer alloc] initWithTarget:self.fridgeItemUnit action:#selector(onTap)];
[self.fridgeItemUnit addGestureRecognizer:tapForUnit];
The above code is for one of the labels. I have removed the code for the view because my labels would stop working, but it's exactly the same, only thing different is that it is attached to self.view and the function that is executed is this one:
-(void)dismissKeyboard:(UIGestureRecognizer*)gesture {
[self.fridgeItemName resignFirstResponder];
[self.fridgeItemQuantity resignFirstResponder];
}
I would implement the following method from UIGestureRecognizerDelegate:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
I attach an example:
The only thing I did on the XIB was to enabled the user interaction. An here is the .m of the UIViewController:
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_viewRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(viewTap:)];
[_viewRecognizer setDelegate:self];
_labelRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(labelTap:)];
[_labelRecognizer setDelegate:self];
[self.view addGestureRecognizer:_viewRecognizer];
[self.label addGestureRecognizer:_labelRecognizer];
}
- (void)viewDidUnload {
[super viewDidUnload];
[_viewRecognizer release]; _viewRecognizer = nil;
[_labelRecognizer release]; _labelRecognizer = nil;
self.label = nil;
}
- (void)dealloc {
[_viewRecognizer release];
[_labelRecognizer release];
self.label = nil;
[super dealloc];
}
- (void)labelTap:(UIGestureRecognizer *)recognizer {
NSLog(#"labelTap");
}
- (void)viewTap:(UIGestureRecognizer *)recognizer {
NSLog(#"viewTap");
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
NSLog(#"shouldRecognizeSimultaneouslyWithGestureRecognizer");
return YES;
}
Then when tapping on the label I get the following log:
shouldRecognizeSimultaneouslyWithGestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer
labelTap
viewTap
And when tapping on the view:
viewTap

Resources