Search code examples
iosiphoneios7uisearchbaruitextinput

UITextInputTraits is not working in ios7


I am new to ios. I have the following code to change the return key type of keyboard to join. It works fine in ios6 but not in ios7. The code inside the if-block is never executed on ios7.

Have anyone seen a similar issue? Is there any workaround for this?

if ([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)]) {
    [searchBar resignFirstResponder];
    [(UITextField *)searchBarSubview setReturnKeyType:UIReturnKeyJoin];
    [searchBar becomeFirstResponder];
}

I googled and found that Join button isn't showing up because the internal structure of UISearchBar, which aren't supposed to modify, has changed. I dont konw how to fix this and where to found out what has changed, Anyone can explain ?

Also tried following(similar way) workaround code not lucky enough

for(UIView *searchBarSubview in [searchBar subviews]) {
if([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)]) {
     [(UITextField *)searchBarSubview setReturnKeyType: UIReturnKeyJoin];
} else {
    for(UIView *subSubView in [searchBarSubview subviews]) {
        if([subSubView conformsToProtocol:@protocol(UITextInputTraits)]) {
            [(UITextField *)subSubView setReturnKeyType: UIReturnKeyJoin];
    }
}      
} 

Solution

  • Try this :

        UITextField *searchBarTextField ;
        NSArray *views = ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0f) ?      
        self.searchBar.subviews : [[self.searchBar.subviews
        objectAtIndex:0] subviews];
        for (UIView *subview in views)
       {
        if ([subview isKindOfClass:[UITextField class]])
        {
            searchBarTextField = (UITextField *)subview;
            break;
        }
      }
       searchBarTextField.returnKeyType = UIReturnKeyJoin;