美文网首页iOS高手
iOS 13 UISearchBar 获取UITextField

iOS 13 UISearchBar 获取UITextField

作者: 阿拉斯加的狗 | 来源:发表于2019-10-30 15:18 被阅读0次

在iOS 13 之前都是通过

[self.searchBar valueForKey:@"_searchField"];

iOS 13 就直接废弃了 直接通过控件的私有属性获取控件方法

  • 所以可以通过这种形式获取
UITextField *searchField = (UITextField*)[self.searchBar findSubview:@"UITextField" resursion:YES];

searchField.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15];

searchField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 0, 0)];

通过UIView的分类 遍历 查询 UITextField


- (UIView *)findSubview:(NSString *)name resursion:(BOOL)resursion {
    Class class = NSClassFromString(name);
    for (UIView *subview in self.subviews) {
        if ([subview isKindOfClass:class]) {
            return subview;
        }
    }
    
    if (resursion) {
        for (UIView *subview in self.subviews) {
            UIView *tempView = [subview findSubview:name resursion:resursion];
            if (tempView) {
                return tempView;
            }
        }
    }
    return nil;
}

相关文章

网友评论

    本文标题:iOS 13 UISearchBar 获取UITextField

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