IOS Custom Back button to keep the system sliding back

Source: Internet
Author: User

original link
The custom Back button retains the system swipe back gesture. Gif1. Introduction

Using the iphone, the favorite is to use its sliding back. As a developer, when we write a lot of pages, there is always a reason why the system's sliding back is not available. Use the navigation bar to push out a controller, We have customized a return button in the controller. This way the system defaults to the swipe back gesture effect is gone.

2. Workaround

[1] From a This controller push to b this controller, we want to customize the B's Return button, we can set in a

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"王俨" style:UIBarButtonItemStylePlain target:nil action:nil];

[2] in the B Controller set back button, I think this should be the choice most people like, but when we set in the B controller, we self.navigationItem.leftBarButtonItem found that the system's sliding return gesture failed. So how can you keep the system sliding back gesture, we can do this in the B controller.

@interfaceViewcontroller () <Uigesturerecognizerdelegate>@end@implementationViewcontroller {id<Uigesturerecognizerdelegate> _delegate}-(void) Viewdidload {[Super Viewdidload];Custom Back buttonUIButton *button = [[UIButton Alloc]initwithframe:CGRectMake (0,0,44,44)]; [Button Settitle:@ "Wang Yu 2" forstate:UIControlStateNormal]; [Button settitlecolor:[Uicolor Blackcolor] Forstate:UIControlStateNormal]; [Button AddTarget:Self action:@selector (back:) forControlEvents:UIControlEventTouchUpInside];Self. Navigationitem. Leftbarbuttonitem = [[Uibarbuttonitem Alloc]initwithcustomview:button];} - (void) Back: (UIButton *) button {[Self. Navigationcontroller popviewcontrolleranimated:YES];} - (void) Viewwillappear: (BOOL) Animated {[Super viewwillappear:animated];if (Self. Navigationcontroller. viewcontrollers. Count >1) {Record system return gesture proxy _delegate =Self. Navigationcontroller. Interactivepopgesturerecognizer. Delegate;Set the system return gesture for the agent for the current controllerSelf. Navigationcontroller. Interactivepopgesturerecognizer. Delegate =Self }}- (void) Viewwilldisappear: (BOOL) Animated {[Super viewwilldisappear:animated];The agent that sets the system return gesture is the return gesture agent for the system that we recorded when we first entered the controller.Self. Navigationcontroller. Interactivepopgesturerecognizer. Delegate = _delegate;}#pragma mark-uigesturerecognizerdelegate-(bool) Gesturerecognizershouldbegin: (UIGestureRecognizer *) Gesturerecognizer {return self .navigationcontroller.childviewcontrollers.count > 1;} -(bool) Gesturerecognizer: (uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: ( Uigesturerecognizer *) Othergesturerecognizer {return self< Span class= "hljs-variable" >.navigationcontroller.viewcontrollers .count > 1;}  @end             

IOS Custom Back button, leave the system sliding back

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.