1

In my iOS App , i need to set custom inputView for UISearchBar in iOS7.

So i wrote following codes.

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

It is working fine. However when i type with my custom keyboard and tap Cancel Button to resignFirstResponder.

And i tap UISearchBar again, i can't type any text in UISearchBar , including native english keyboard.

And also Cancel Button is hiding and UISearchBar is not working anymore.

I don't know why is happening?

How can i solve it?

Fire Fist
  • 7,032
  • 12
  • 63
  • 109

1 Answers1

1

I tried your code and it's working fine with my app. In fact thanks and congratulations seem to be in order, because for one reason or other you seem to have solved a problem others on this site had to built more elaborate workarounds for.

In my app the keyboard changes according to the selected scope of the searchbar:

  • scope == 0 presents my CustomKeyboard
  • scope != 0 presents the usual iPhone Keyboard.

My implementation of your code looks like this:

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

Probably not the most efficient way to do it, but it does the trick. I hope you'll find something that helps you solve your problem.

magnus1969
  • 137
  • 6