Enable Search button when searching string is empty in default search bar Enable Search button when searching string is empty in default search bar ios ios

Enable Search button when searching string is empty in default search bar


Actually you can just set searchBar.enablesReturnKeyAutomatically = NO; Tested on iOS 7+


This code display Search Button if you have empty string.

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {    [self.searchBar setShowsCancelButton:YES animated:YES];    self.tblView.allowsSelection = NO;    self.tblView.scrollEnabled = NO;        UITextField *searchBarTextField = nil;    for (UIView *subview in self.searchBar.subviews)    {        if ([subview isKindOfClass:[UITextField class]])        {            searchBarTextField = (UITextField *)subview;            break;        }    }    searchBarTextField.enablesReturnKeyAutomatically = NO;}


Swift 3/ iOS 10

for view1 in searchBar.subviews {        for view2 in view1.subviews {            if let searchBarTextField = view2 as? UITextField {                searchBarTextField.enablesReturnKeyAutomatically = false                break            }        }    }


matomo