美文网首页
iOS去掉导航栏返回按钮的文字(Method Swizzling

iOS去掉导航栏返回按钮的文字(Method Swizzling

作者: 街角仰望 | 来源:发表于2020-06-12 17:35 被阅读0次

方法一

1、自定义UINavigationController
2、遵守 <UINavigationBarDelegate>协议
3、实现下面方法:

#pragma mark --------- UINavigationBarDelegate

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item {
    
    //设置导航栏返回按钮文字
    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;
}

注意:该方法会出现部分子控制器页面的返回按钮文字出现的bug,需要在其子控制器页面的父控制器里再次如上设置返回按钮才行

子控制器页面的父控制器

#pragma mark -------- 生命周期函数

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //重新设置下级子页面导航栏返回按钮文字
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = item;

}

方法二

1、自定义UINavigationController
2、遵守 <UINavigationBarDelegate>协议
3、实现下面方法:

#pragma mark --------- UINavigationBarDelegate

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item {
    
    //设置导航栏返回按钮文字为透明的,可能造成导航标题不居中的问题
    [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];
    [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateHighlighted];
    
    return YES;
}

方法三(推荐)

1、给UIViewController添加类别(这里的类别不需要导入可直接使用)
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));
}
 
//设置导航栏返回按钮文字
- (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

相关文章

网友评论

      本文标题:iOS去掉导航栏返回按钮的文字(Method Swizzling

      本文链接:https://www.haomeiwen.com/subject/mnnqahtx.html