iOS NavigationBar 导航栏背景颜色设置方案探究

参考地址:http://www.jianshu.com/p/6a5552ec5099

在上篇文章   四-2 中的代码中加入以下代码进行完善

使用runtime进行方法的替换操作。if判断(是因为有的类并没有对应的方法,是其父类的方法,所以需要自己再给本类添加一个方法的实现)

static const char overlayKey;

// 把类加载进内存的时候调用,只会调用一次

+ (void)load

{

Method setBack = class_getInstanceMethod([self class], @selector(setBackgroundColor:));

Method kpSetBack = class_getInstanceMethod([self class], @selector(KPSetBackgroundColor:));

class_replaceMethod(self, @selector(KPSetBackgroundColor:), method_getImplementation(setBack), method_getTypeEncoding(setBack));

BOOL successAdded = class_addMethod(self, @selector(setBackgroundColor:), method_getImplementation(kpSetBack), method_getTypeEncoding(kpSetBack));

if (successAdded) {

class_replaceMethod(self, @selector(KPSetBackgroundColor:), method_getImplementation(setBack), method_getTypeEncoding(setBack));

} else {

method_exchangeImplementations(setBack, kpSetBack);

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,859评论 0 9
  • 本文转载自:http://yulingtianxia.com/blog/2014/11/05/objective-...
    ant_flex阅读 819评论 0 1
  • 转载:http://www.cocoachina.com/ios/20161102/17920.html 因为Ob...
    F麦子阅读 677评论 0 1
  • 我们常常会听说 Objective-C 是一门动态语言,那么这个「动态」表现在哪呢?我想最主要的表现就是 Obje...
    Ethan_Struggle阅读 2,244评论 0 7
  • 本文详细整理了 Cocoa 的 Runtime 系统的知识,它使得 Objective-C 如虎添翼,具备了灵活的...
    lylaut阅读 844评论 0 4