1

在我的 iOS App 中,我需要inputViewUISearchBarin设置自定义iOS7

所以我写了以下代码。

NSArray *searchBarSubViews = [[self.sBar.subviews objectAtIndex:0] subviews];
            for(int i =0; i<[searchBarSubViews count]; i++) {
                if([[searchBarSubViews objectAtIndex:i] isKindOfClass:[UITextField class]])
                {
                    UITextField* search=(UITextField*)[searchBarSubViews objectAtIndex:i];
                    [search setFont:[UIFont fontWithName:@"CustomFont" size:15]];
                    search.delegate = self;

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

它工作正常。但是,当我使用自定义键盘键入并点击取消按钮时resignFirstResponder

UISearchBar再次点击,我无法输入任何文字UISearchBar,包括母语英文键盘。

并且取消按钮也被隐藏并且UISearchBar不再工作了。

我不知道为什么会这样?

我该如何解决?

4

1 回答 1

1

我尝试了您的代码,它在我的应用程序中运行良好。事实上,感谢和祝贺似乎是有序的,因为出于某种原因,您似乎已经解决了该站点上的其他人不得不构建更复杂的解决方法的问题。

在我的应用程序中,键盘会根据搜索栏的选定范围而变化:

  • scope == 0 代表我的 CustomKeyboard
  • scope != 0 表示通常的 iPhone 键盘。

我的代码实现如下所示:

-(UITextField *)textFieldFormSearchBar{

NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews];
for(int i =0; i<[searchBarSubViews count]; i++) {
    if([[searchBarSubViews objectAtIndex:i] isKindOfClass:[UITextField class]])
    {
        UITextField* search=(UITextField*)[searchBarSubViews objectAtIndex:i];
        return search;
    }
}
return nil;
}

-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{

if (searchBar.selectedScopeButtonIndex==0) {
    self.textFieldFormSearchBar.delegate = self;
    self.textFieldFormSearchBar.inputView=self.customKeyboard;
    self.customKeyboard.field=self.textFieldFormSearchBar;
}
else{
    if (self.customKeyboard.superview!=nil) [self.customKeyboard removeFromSuperview];
    self.textFieldFormSearchBar.delegate = self;
    self.textFieldFormSearchBar.inputView=nil;
    self.customKeyboard.field=nil;
}
return YES;
}

-(void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{

if (selectedScope==0) {
    self.textFieldFormSearchBar.delegate = self;
    self.textFieldFormSearchBar.inputView=self.customKeyboard;
    self.customKeyboard.field=self.textFieldFormSearchBar;
}
else{
    if (self.customKeyboard.superview!=nil) [self.customKeyboard removeFromSuperview];
    self.textFieldFormSearchBar.delegate = self;
    self.textFieldFormSearchBar.inputView=nil;
    self.customKeyboard.field=nil;
}
[self.textFieldFormSearchBar reloadInputViews];
}

-(BOOL)searchBarShouldEndEditing:(HDLSearchBar *)searchBar{

if (self.customKeyboard) {
    [self.customKeyboard removeFromSuperview];
}
return [self.textFieldFormSearchBar resignFirstResponder];
}

可能不是最有效的方法,但它确实有效。我希望你能找到一些可以帮助你解决问题的东西。

于 2013-11-03T10:34:51.267 回答