Đây là giải pháp của tôi:
Trong appdelegate (các "chủ sở hữu" của thanh nav trong ứng dụng của tôi), trong applicationDidFinishLaunchingWithOptions:
Lấy cái nhìn thanh nav và thêm các nhận dạng cử chỉ cho toàn bộ quan điểm:
// 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.");
Sau đó, trong appDel eGate, trong - (void) backButtonLongPress: (id) sender
Kiểm tra để xem nếu cử chỉ xảy ra trong khuôn khổ các nút quay lại:
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
}
Thêm một nhận dạng cử chỉ với một giao diện tùy chỉnh trên backButtomItem didn 't làm việc cho tôi ... người thừa nhận từ chối bắn. Bạn có thể làm cho nó hoạt động với mã ở trên không? – kevboh
Có thể không hoạt động vì backBarButtonItem chỉ đọc, vì vậy nó không chấp nhận chế độ xem tùy chỉnh.Bạn rất có thể cần phải tạo ra leftbarbuttonitem của riêng bạn như câu trả lời này. http://stackoverflow.com/questions/526520/how-to-create-backbarbuttomitem-with-custom-view-for-a-uinavigationcontroller – Andrew
Ah, nhưng sau đó tôi bị mất mũi tên quay lại trừ khi tôi tìm thấy hình ảnh ... có thể không đáng. Cảm ơn dù sao, mặc dù! – kevboh