ios 去掉导航栏返回按钮文字

@interface UIViewController (Navigation)

@end

#import "UIViewController+Navigation.h"


@implementation UIViewController (Navigation)
+(void)load{
    ExchangeImplementations(self, @selector(viewWillAppear:), @selector(jh_viewWillAppear:));
}

-(void)jh_viewWillAppear:(BOOL)animated{
    [self jh_viewWillAppear:animated];
    if(self.navigationController!=nil && self.navigationItem.backBarButtonItem == nil  && self.navigationController.viewControllers.count>0)
    {
        UIBarButtonItem *back = [[UIBarButtonItem alloc] init];
        [back setTitle:@""];
        self.navigationItem.backBarButtonItem = back;
      }
}
@end

CG_INLINE BOOL
ExchangeImplementationsInTwoClasses(Class _fromClass, SEL _originSelector, Class _toClass, SEL _newSelector) {
    if (!_fromClass || !_toClass) {
        return NO;
    }
    
    Method oriMethod = class_getInstanceMethod(_fromClass, _originSelector);
    Method newMethod = class_getInstanceMethod(_toClass, _newSelector);
    if (!newMethod) {
        return NO;
    }
    
    BOOL isAddedMethod = class_addMethod(_fromClass, _originSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod));
    if (isAddedMethod) {
        // 如果 class_addMethod 成功了,说明之前 fromClass 里并不存在 originSelector,所以要用一个空的方法代替它,以避免 class_replaceMethod 后,后续 toClass 的这个方法被调用时可能会 crash
        IMP oriMethodIMP = method_getImplementation(oriMethod) ?: imp_implementationWithBlock(^(id selfObject) {});
        const char *oriMethodTypeEncoding = method_getTypeEncoding(oriMethod) ?: "v@:";
        class_replaceMethod(_toClass, _newSelector, oriMethodIMP, oriMethodTypeEncoding);
    } else {
        method_exchangeImplementations(oriMethod, newMethod);
    }
    return YES;
}

CG_INLINE BOOL
ExchangeImplementations(Class _class, SEL _originSelector, SEL _newSelector) {
    return ExchangeImplementationsInTwoClasses(_class, _originSelector, _class, _newSelector);
}
发布了47 篇原创文章 · 获赞 7 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/a1034386099/article/details/103478804