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).
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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With