如何改变的UISearchBar的inputView在iOS7?(How to change inp

2019-09-26 13:09发布

在iOS6的,我用下面的代码改变的UISearchBar的inputView

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

在的UISearchBar iOS7发生了变化,我不知道如何找到文本框来改变inputView。 请帮忙! 谢谢!

Answer 1:

子视图的层次结构中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];


Answer 2:

使用下面的代码:

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


Answer 3:

试试这个代码:

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

在iOS的7子视图不工作。 从iOS7 UI对象都有一个额外的包装/容器。 可能是这个代码将帮助你。



文章来源: How to change inputView of UISearchBar in iOS7?