UISearchBar的自定义InputView在iOS7中不起作用[英] Custom InputView for UISearchBar doesn't work in iOS7

本文是小编为大家收集整理的关于UISearchBar的自定义InputView在iOS7中不起作用的处理方法,想解了UISearchBar的自定义InputView在iOS7中不起作用的问题怎么解决?UISearchBar的自定义InputView在iOS7中不起作用问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

我需要在 UISearchBar 中设置我的自定义 inputView.

所以我编写了以下代码,在 iOS5 和 iOS6 中完美运行.

但它在 iOS7 中不起作用.

for(int i =0; i<[self.searchBar.subviews count]; i++) {
            if([[self.searchBar.subviews objectAtIndex:i] isKindOfClass:[UITextField class]])
            {
                [(UITextField*)[searchBar.subviews objectAtIndex:i] setFont:[UIFont fontWithName:@"MyCustomFont" size:15]];
                UITextField* search=(UITextField*)[searchBar.subviews objectAtIndex:i];
                search.delegate = self;

                [(UITextField*)[self.searchBar.subviews objectAtIndex:i] setInputView:self.customKeyboard];
                [self.customKeyboard setTextView:search];
            }
        }



        [self.sBar reloadInputViews];

我该怎么做?

推荐答案

iOS7 中的 searchBar 子视图层次结构发生了变化,试试下面的方法:

iOS7:

NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews];

iOS6 及之前:

NSArray *searchBarSubViews =  self.searchBar.subviews;

试试下面的更新功能:

NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews];
for(int i =0; i<[searchBarSubViews count]; i++) {
    if([[searchBarSubViews objectAtIndex:i] isKindOfClass:[UITextField class]])
    {
        UITextField* search=(UITextField*)[searchBarSubViews objectAtIndex:i];
        [search setFont:[UIFont fontWithName:@"MyCustomFont" size:15]];
        search.delegate = self;

        [search setInputView:self.customKeyboard];
        [self.customKeyboard setTextView:search];
    }
}
[self.sBar reloadInputViews];

本文地址:https://www.itbaoku.cn/post/924418.html