iOS 13 修改私有属性“_placeholderLabel”解决思路

iOS13 使用以下两个方法出现crash

- (void)setValue:(nullable id)value forKeyPath:(NSString *)keyPath
 - (nullable id)valueForKeyPath:(NSString *)keyPath

以UITextField为例解决思路如下:

#import <objc/runtime.h>

- (void)updatePlaceholderColor {
 Ivar ivar = class_getInstanceVariable([textField class], "_placeholderLabel");
 id placeholderLabel = object_getIvar(textField, ivar);
 
 Ivar ivar_1 = class_getInstanceVariable([placeholderLabel class], "_defaultAttributes");
 NSMutableDictionary *defaultTextAttributes = object_getIvar(placeholderLabel, ivar_1);
 [defaultTextAttributes setObject:UIColor.redColor forKey:@"NSColor"];
 // 或者以下一句
 // [placeholderLabel performSelector:@selector(setTextColor:) withObject:UIColor.redColor];
}

注:对应的关键字可以通过runtime打印,也可以xcode断点调试查看

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

推荐阅读更多精彩内容

  • 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。--《青玉案·元夕》 要学会看crash崩溃和报告 一个应用程序并不...
    欧阳大哥2013阅读 22,195评论 38 232
  • 花,有重开日; 人,无再少年。
    风与风寻阅读 1,241评论 0 0
  • 第一章 军训记 念中学的时候,就没军训过,所以一进大学,就挺向往穿上军装,扛起枪,参加军训。 报了名,领了军装,军...
    黄辉huihuang阅读 3,043评论 0 0