iOS 13适配问题

846 阅读1分钟

目前在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];
}