前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >去掉 iOS 导航栏返回按钮文本三种方案

去掉 iOS 导航栏返回按钮文本三种方案

作者头像
网罗开发
发布2021-06-24 16:14:01
2.4K0
发布2021-06-24 16:14:01
举报
文章被收录于专栏:网罗开发

方案一

  1. 自定义 UINavigationController
  2. 遵守 UINavigationBarDel 协议
  3. 实现下面方法:
代码语言:javascript
复制
#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,需要在其子控制器页面的父控制器里再次如上设置返回按钮才行

代码语言:javascript
复制
子控制器页面的父控制器

#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. 实现下面方法:
代码语言:javascript
复制
#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 方法,代码如下:
代码语言:javascript
复制
#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

-End-

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-06-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 网罗开发 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 方案一
  • 方案二
  • 方案三(推荐)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档