Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In my application I am using search functionality using default IOS search bar, If i place some string for search its working fine but after the first search i need to display the entire data Source (original content) My functionality is if the search string is empty it will display the entire data source. My issue is if i make the search string as empty in default search bar, the search button automatically come to hide state. I need to enable the search button even the string is empty.

like image 811
Ganapathy Avatar asked Jan 11 '13 05:01

Ganapathy


4 Answers

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

like image 131
laura Avatar answered Nov 06 '22 07:11

laura


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;
}
like image 27
iPatel Avatar answered Nov 06 '22 06:11

iPatel


Swift 3/ iOS 10

for view1 in searchBar.subviews {
        for view2 in view1.subviews {
            if let searchBarTextField = view2 as? UITextField {
                searchBarTextField.enablesReturnKeyAutomatically = false
                break
            }
        }
    }
like image 4
Alocus Avatar answered Nov 06 '22 06:11

Alocus


Use the following code for enable return key with no text

  UITextField *searchField = nil;
  for (UIView *subview in searchBar.subviews) {
    if ([subview isKindOfClass:[UITextField class]]) {
      searchField = (UITextField *)subview;
      break;
    }
  }

  if (searchField) {
    searchField.enablesReturnKeyAutomatically = NO;
  }
like image 3
MilanPanchal Avatar answered Nov 06 '22 05:11

MilanPanchal