iOS去掉導(dǎo)航欄返回按鈕的文字(Method Swizzling)

方法一

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

參考:https://www.cnblogs.com/Apple2U/p/8991662.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容