سؤال

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!

هل كانت مفيدة؟

المحلول

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];

نصائح أخرى

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];

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top