1

In iOS6 , I use the below code to change inputView of UISearchbar

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

UISearchbar in iOS7 had changed, I don't know how to find textField to change inputView. Please help! Thanks!

LE SANG
  • 10,955
  • 7
  • 59
  • 78

3 Answers3

2

The hierarchy of subview has been changed in iOS7, so you can use the following code:

// 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];
LE SANG
  • 10,955
  • 7
  • 59
  • 78
Vinay Jain
  • 2,644
  • 3
  • 26
  • 44
  • 1
    You can get the UITextField from the UISearchBar also with this command: [self.searchBar valueForKey:@"_searchField"] – matzino Mar 21 '14 at 14:49
1

Use following code :

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];
iPatel
  • 46,010
  • 16
  • 115
  • 137
0

Try this code :

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

in iOS 7 subview is not working. From iOS7 UI objects have a extra wrapper/container. May be this code will help you.

Sudha Tiwari
  • 2,499
  • 26
  • 50