方法一
1、自定義UINavigationController
2、遵守 <UINavigationBarDelegate>協(xié)議
3、實(shí)現(xiàn)下面方法:
#pragma mark --------- UINavigationBarDelegate
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item {
//設(shè)置導(dǎo)航欄返回按鈕文字
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:nil action:nil];
/*
NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
textAttrs[UITextAttributeTextColor] = [UIColor whiteColor];
[back setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
*/
item.backBarButtonItem = back;
return YES;
}
注意:該方法會出現(xiàn)部分子控制器頁面的返回按鈕文字出現(xiàn)的bug,需要在其子控制器頁面的父控制器里再次如上設(shè)置返回按鈕才行
子控制器頁面的父控制器
#pragma mark -------- 生命周期函數(shù)
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
//重新設(shè)置下級子頁面導(dǎo)航欄返回按鈕文字
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = item;
}
方法二
1、自定義UINavigationController
2、遵守 <UINavigationBarDelegate>協(xié)議
3、實(shí)現(xiàn)下面方法:
#pragma mark --------- UINavigationBarDelegate
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item {
//設(shè)置導(dǎo)航欄返回按鈕文字為透明的,可能造成導(dǎo)航標(biāo)題不居中的問題
[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];
[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateHighlighted];
return YES;
}
方法三(推薦)
1、給UIViewController添加類別(這里的類別不需要導(dǎo)入可直接使用)
2、然后在load方法里面用Method Swzilling方法替換交換ViewDidAppear方法,代碼如下:
#import "UIViewController+HideNavBackTitle.h"
#import <objc/runtime.h>
@implementation UIViewController (HideNavBackTitle)
+(void)load {
swizzleMethod([self class], @selector(viewDidAppear:), @selector(ac_viewDidAppear));
}
//設(shè)置導(dǎo)航欄返回按鈕文字
- (void)ac_viewDidAppear{
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:@""
style:UIBarButtonItemStylePlain
target:self
action:nil];
[self ac_viewDidAppear];
}
void swizzleMethod(Class class, SEL originalSelector, SEL swizzledSelector)
{
// the method might not exist in the class, but in its superclass
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
// class_addMethod will fail if original method already exists
BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
// the method doesn’t exist and we just added one
if (didAddMethod) {
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
}
else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
@end