Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom back button title and keep the swipe back gesture

Problem : I would like to custom the navigation back button title in the popped view controller like Whatsapp ( < Chats (2) / < Chats (3) ).

However to assign a new backBarButtonItem in the popped view controller will disable the swipe back gesture, if you use

self.navigationController.interactivePopGestureRecognizer.delegate = self;

to keep the gesture work, it will give you more troublessss (too many bugssss).

like image 433
Rex Lam Avatar asked Oct 31 '22 23:10

Rex Lam


2 Answers

You have to set the self.navigationItem.backBarButtonItem property on the ViewController that comes before the one will show the title.

In the Whatsapp example, you will have to set the title on the chats list view controller.

Something like that:

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"chats(2)" style:UIBarButtonItemStylePlain target:nil action:nil];

After that, you can set just the title of self.navigationItem.backBarButtonItem.

like image 186
Gonzo Avatar answered Nov 08 '22 09:11

Gonzo


If you want to empty back button title in whole of application, one of the solutions is to swizzle viewDidLoad and empty back button title in the swizzled viewDidLoad. And it won't affect to interactivePopGestureRecognizer's work (make sure interactivePopGestureRecognizer is enabled).

            @implementation UIViewController (Customizations)

            + (void)load {
                static dispatch_once_t onceToken;
                dispatch_once(&onceToken, ^{
                    [UIViewController swizzleClass:[UIViewController class] method:@"viewDidLoad"];
                });
            }

            + (void)swizzleClass:(Class)class method:(NSString*)methodName {
                SEL originalMethod = NSSelectorFromString(methodName);
                SEL newMethod = NSSelectorFromString([NSString stringWithFormat:@"%@%@", @"override_", methodName]);
                [self swizzle:class from:originalMethod to:newMethod];
            }

            + (void)swizzle:(Class)class from:(SEL)original to:(SEL)new {
                Method originalMethod = class_getInstanceMethod(class, original);
                Method newMethod = class_getInstanceMethod(class, new);
                if(class_addMethod(class, original, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))) {
                    class_replaceMethod(class, new, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
                } else {
                    method_exchangeImplementations(originalMethod, newMethod);
                }
            }

            - (void)override_viewDidLoad {
                //Empty back button title
                UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
                [self.navigationItem setBackBarButtonItem:backButtonItem];
                [self override_viewDidLoad];
            }

            @end
like image 31
Narek Safaryan Avatar answered Nov 08 '22 08:11

Narek Safaryan