uitextfield - 获取搜索栏的文本字段 iOS 7.1

标签 uitextfield uisearchbar ios7.1

有谁知道如何从 iOS 7.1 上的任何 UISearchBar 获取 UITextField,我需要更改字体和搜索图标,但旧代码不适用于新版本 7.1,感谢您的帮助。

这是获取 UITextField 的旧代码,仅适用于低于 7.1 的版本

UITextField *txfSearchField = [default_search_bar valueForKeyPath:@"_searchField"];

最佳答案

如果您属于 UISearchBar 的子类您可以使用简单的修复。

不知何故,初始化后 subview 中没有 UITextField 的实例。 但在 - (void)layoutSubviews 中你可以找到它。

所以解决方案是将搜索文本字段的代码移出 init。 您可以使用这个简单的方法,该方法适用于 7.1 之前的所有 iOS

- (UIView *)firstViewOfClass:(Class)class inSubviewsOfView:(UIView *)view
{
    UIView *result = nil;
    for (UIView *subview in view.subviews)
    {
        if ([subview isKindOfClass:class])
        {
            result =  subview;
    }
    else
    {
        result = [self firstViewOfClass:class inSubviewsOfView:subview];
    }
    if (result)
    {
        break;
    }
}
return result;

}

使用示例:

- (void)layoutSubviews
{
    if (!self.textField)
    {
        self.textField = (UITextField *)[self firstViewOfClass:[UITextField class] inSubviewsOfView:self];
    }
    [super layoutSubviews];

    // your code here.
}

关于uitextfield - 获取搜索栏的文本字段 iOS 7.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22366476/

相关文章:

ios - 当用户触摸 UIBarButtonItem 时,在 tableView 上显示 UISearchBar

ios - 如何从搜索结果 DetailView 返回 UITableView

ios - self.navigationItem.titleView 不适用于选项卡式应用程序模板

ios - SLComposeViewController 深色键盘外观

ios - 如何移动文本框?

swift - Swift 中 UITextfield 字符数限制

IOS 搜索键盘慢

iOS 7.1 更新在 uitextfields 之间切换时崩溃,原因为 setObjectForKey : object cannot be nil (key: NSShadow)

objective-c - 无法在 iOS7 上将 UITextField 添加到 UIAlertView ...在 iOS 6 中有效

ios7 - 将 inputAccessoryView - UIToolBar 上 UITextField 上的文本镜像到 navigationController.toolbar 上 UITextField 上的文本