How to change inputView of UISearchBar in iOS7?

大兔子大兔子 提交于 2019-12-13 02:16:19

问题


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!


回答1:


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



回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/20278677/how-to-change-inputview-of-uisearchbar-in-ios7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!