iOS13中通过KVC方式来获取私有属性崩溃
1.私有API被封禁(KVC限制),禁止访问.
iOS13中通过KVC方式来获取私有属性,有Carsh风险,尽量避免使用.比如我们常用的UITextFiled和UISearchController等,在iOS 13的searchbar添加了一个- (void)set_cancelButtonText:(NSString *)text方法,这个方法专门用来命中kvc,一旦命中就Crash。
//修改textField的占位符字体颜色
textField setValue:[UIColor xxx] forKeyPath:@"_placeholderLabel.textColor"];//Crash
1.获取SearchBar的textField
由于在13中把SearchBar中的textField直接暴露给开发者使用,无需在通过kvc获取。
- (UITextField *)sa_GetSearchTextFiled{
if ([[[UIDevice currentDevice]systemVersion] floatValue] >= 13.0) {
return self.searchTextField;
}else{
UITextField *searchTextField = [self valueForKey:@"_searchField"];
return searchTextField;
}
}
2.修改TextFiled的占位符字体大小以及颜色,在iOS13中不能通过KVC来进行修改,可以通过其属性字符串来进行修改
//[selfdrawPlaceholderInTF:self.sign_in_usernameTF phColor:kLabelTextColor fontSize:15];
//textField 设置 placeholderColor 方法
-(void)drawPlaceholderInTF:(UITextField*)textField phColor:(UIColor*)phColor fontSize:(CGFloat)fontSize{
NSMutableAttributedString *arrStr = [[NSMutableAttributedString alloc]initWithString:textField.placeholder attributes:@{NSForegroundColorAttributeName : phColor ? phColor : [UIColor lightGrayColor],NSFontAttributeName:[UIFont systemFontOfSize:fontSize]}];
textField.attributedPlaceholder= arrStr;
}
网友评论