iOS 全局修改UINavigation 后退按钮

 

将导航栏的后退按钮中的文字去掉一直是老大难问题,现在可以使用运行时机制,将后退按钮文字清空

创建UINavigationItem的category,直接上代码

.h文件

#import <UIKit/UIKit.h>

@interface UINavigationItem (CustomBackButton)
@end

  

.m文件

#import "UINavigationItem+CustomBackButton.h"
#import <objc/runtime.h>

@implementation UINavigationItem (CustomBackButton)

+(void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method originalMethodImp = class_getInstanceMethod(self, @selector(backBarButtonItem));
        Method destMethodImp = class_getInstanceMethod(self, @selector(myCustomBackButton_backBarbuttonItem));
        method_exchangeImplementations(originalMethodImp, destMethodImp);
    });
}

-(UIBarButtonItem *)backBarButtonItem{
    return [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:nil action:NULL];
}

static char kCustomBackButtonKey;
-(UIBarButtonItem *)myCustomBackButton_backBarbuttonItem{
    UIBarButtonItem *item = [self myCustomBackButton_backBarbuttonItem];
    if (item) {
        return item;
    }
    item = objc_getAssociatedObject(self, &kCustomBackButtonKey);
    if (!item) {
        item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:nil action:NULL];
        objc_setAssociatedObject(self, &kCustomBackButtonKey, item, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return item;
}

- (void)dealloc {
    objc_removeAssociatedObjects(self);
}

@end

注意

#import <objc/runtime.h>

 

然后在Appdelegate中导入即可

#import "UINavigationItem+CustomBackButton.h"

 

原文链接:http://blog.csdn.net/zhangao0086/article/details/22432387

posted @ 2016-06-08 15:41  柳云居士  阅读(1732)  评论(0编辑  收藏  举报