iOS 13适配问题

·  阅读 540

目前在iOS 13上运行出现报错

 *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Access to UITextField's _placeholderLabel ivar is prohibited. This is an application bug'
*** First throw call stack:
复制代码

这是由于iOS 13的UITextField,开发者试图通过KVC修改私有属性的时候报错

[self.myTestTextField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
复制代码

解决方案: 可以通过runtime方式进行获取私有属性,进行修改

#import <objc/runtime.h>

- (IBAction)changeColorClick:(id)sender {
//	[self.myTestTextField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
	
	Ivar ivar = class_getInstanceVariable([UITextField class], "_placeholderLabel");
	UILabel *myPlaceholderLabel = object_getIvar(self.myTestTextField, ivar);
	myPlaceholderLabel.textColor = [UIColor orangeColor];
}

复制代码
分类:
iOS
标签:
分类:
iOS
标签:
收藏成功!
已添加到「」, 点击更改