如何在iOS7中更改UISearchBar的inputView?

时间:2013-11-29 05:02:08

标签: ios ios7 uisearchbar inputview

在iOS6中,我使用下面的代码来更改UISearchbar的inputView

for (UIView *view in _searchBar.subviews) {
    if ([view isKindOfClass: [UITextField class]]) {
        UITextField* textField = (UITextField*)view;
        [textField setInputView:_myKeyboard];
        break;
    }
}

iOS7中的UISearchbar已经改变,我不知道如何找到textField来改变inputView。 请帮忙!谢谢!

3 个答案:

答案 0 :(得分:2)

在iOS7中更改了子视图的层次结构,因此您可以使用以下代码:

// subviews
NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews];
for (UIView *view in searchBarSubViews) {
    if([view isKindOfClass:[UITextField class]])
    {
        UITextField* search=(UITextField*)view;
        [search setFont:[UIFont fontWithName:@"MyCustomFont" size:15]];
        search.delegate = self;

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

答案 1 :(得分:1)

使用以下代码:

NSArray *subViewsOfSearchBar = [[self.YOurSearchBar.subviews objectAtIndex:0] subviews];
for(int i =0; i< subViewsOfSearchBar.count; i++) {
    if([[subViewsOfSearchBar objectAtIndex:i] isKindOfClass:[UITextField class]])
    {
        UITextField *searchTxtField=(UITextField*)[subViewsOfSearchBar objectAtIndex:i];
        [searchTxtField setInputView:self.customKeyboard];
    }
}
[self.searchBar reloadInputViews];

答案 2 :(得分:0)

试试这段代码:

for (UIView *view in _searchBar.inputView.subviews) {
        if ([view isKindOfClass: [UITextField class]]) {
            UITextField* textField = (UITextField*)view;
            [textField setInputView:_myKeyboard];
            break;
        }
    }
iOS 7子视图中的

无效。从iOS7 UI对象有一个额外的包装器/容器。可能这段代码会对你有帮助。