aquí está mi solución:
En AppDelegate (la "propietario" de la barra de navegación en mi aplicación), en applicationDidFinishLaunchingWithOptions:
obtener la vista barra de navegación y añadir el reconocedor gesto a la visión de conjunto:
// Get the nav bar view
UINavigationBar *myNavBar = nil;
for (UIView *view in [self.window.rootViewController.view subviews]) {
if ([view isKindOfClass:[UINavigationBar class]]) {
NSLog(@"Found Nav Bar!!!");
myNavBar = (UINavigationBar *)view;
}
}
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self
action:@selector(backButtonLongPress:)];
[myNavBar addGestureRecognizer:longPress];
NSLog(@"Gesture Recognizer Added.");
Luego, en appDel egate, en - (void) backButtonLongPress: (id) emisor
Compruebe si el gesto se produce dentro del marco del botón de retroceso:
if ([sender state] == UIGestureRecognizerStateBegan) {
// Get the nav bar view
UINavigationBar *myNavBar = nil;
for (UIView *view in [self.window.rootViewController.view subviews]) {
if ([view isKindOfClass:[UINavigationBar class]]) {
NSLog(@"Found Nav Bar!!!");
myNavBar = (UINavigationBar *)view;
}
}
// Get the back button view
UIView *backButtonView = nil;
for (UIView *view in [myNavBar subviews]) {
if ([[[view class] description] isEqualToString:@"UINavigationItemButtonView"]) {
backButtonView = view;
NSLog(@"Found It: %@", backButtonView);
NSLog(@"Back Button View Frame: %f, %f; %f, %f", backButtonView.frame.origin.x, backButtonView.frame.origin.y, backButtonView.frame.size.width, backButtonView.frame.size.height);
}
}
CGPoint longPressPoint = [sender locationInView:myNavBar];
NSLog(@"Touch is in back button: %@", CGRectContainsPoint(backButtonView.frame, longPressPoint) ? @"YES" : @"NO");
if (CGRectContainsPoint(backButtonView.frame, longPressPoint)) {
// Place your action here
}
// Do nothing if outside the back button frame
}
Adición de un reconocedor gesto a una vista personalizada en el backButtomItem didn No funciona para mí ... el reconocedor se niega a disparar. ¿Pudiste hacerlo funcionar con el código de arriba? – kevboh
Es probable que no funcione porque el backBarButtonItem es de solo lectura, por lo que no acepta una vista personalizada.Lo más probable es que necesites crear tu propio elemento de barra izquierda como esta respuesta. http://stackoverflow.com/questions/526520/how-to-create-backbarbuttomitem-with-custom-view-for-a-uinavigationcontroller – Andrew
Ah, pero luego pierdo mi flecha trasera a menos que encuentre una imagen ... probablemente no vale la pena. ¡Gracias de todos modos! – kevboh