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);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章