zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    乐观锁和悲观锁
    c++ inline使函数实现可以在头文件中,避免多重定义错误
    无锁队列的实现
    同步异步阻塞非阻塞
    log(m+n)找第k大
    ios开发常见的 工具 - 值得收藏
    iOS 性能优化得 方法
    搭建 安卓开发环境 !
    项目支持 64 - bit
    AFN学习笔记
  • 原文地址:https://www.cnblogs.com/anywherego/p/5570283.html
Copyright © 2011-2022 走看看