美文网首页
iOS NavigationBar 导航栏背景颜色设置方案探究

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

作者: 木槿WEIXIAO | 来源:发表于2017-10-27 10:45 被阅读0次

参考地址: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);

}

}

相关文章

网友评论

      本文标题:iOS NavigationBar 导航栏背景颜色设置方案探究

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