27

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.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Ganapathy
  • 4,594
  • 5
  • 23
  • 41
  • Create your custom search bar. AFAIK, its not possible with native search bar. – Apurv Jan 11 '13 at 05:06
  • you refine search as soon as user type or when user press search button? – CRDave Jan 11 '13 at 05:09
  • Content in the source table is too high. If i do searching like that means it will create performance issue's for me.Data source contains lot of images also. – Ganapathy Jan 11 '13 at 05:15

12 Answers12

37

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

laura
  • 491
  • 4
  • 4
24

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;
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
iPatel
  • 46,010
  • 16
  • 115
  • 137
  • 1
    Hello ! I used to use this code but when I updated to iOS 7 the code has stopped working, and I'm trying to figure why. Has someone had the same problem? – MaikaDalila Oct 04 '13 at 15:54
  • Never mind, a user already found the answer here http://stackoverflow.com/questions/18971988/search-a-uisearchbar-with-no-text-in-xcode5-ios7 – MaikaDalila Oct 04 '13 at 16:08
  • @MaikaDalila - Its good for developer who want to find its on iOS 7.. so thanks for valuable like :) – iPatel Oct 08 '13 at 08:39
4

Swift 3/ iOS 10

for view1 in searchBar.subviews {
        for view2 in view1.subviews {
            if let searchBarTextField = view2 as? UITextField {
                searchBarTextField.enablesReturnKeyAutomatically = false
                break
            }
        }
    }
Alocus
  • 1,860
  • 3
  • 21
  • 32
3

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;
  }
MilanPanchal
  • 2,943
  • 1
  • 19
  • 37
3

Maybe is an apple side bug?. Since from the .xib file setting auto-enable return Key does not work as expected.

Just add the following code:

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    searchBar.enablesReturnKeyAutomatically = NO;

    return YES;
}
Taka
  • 41
  • 4
2

Create a custom view & on that view add one button to remove the keyboard. Add that view when - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar delegate method of UISearchBar. On that button click resign the keyboard as well as the view which you created for that button. Also if you want to search on that button click then you can do it as well. Please see image for more clarification.

enter image description here

Girish
  • 4,692
  • 4
  • 35
  • 55
2

Its almost same as the accepted answer but if You are working with iOS 7 you will need extra for loop due to some changes in search bar

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

        }  
    }
    searchBarTextField.enablesReturnKeyAutomatically = NO;
}
Bhumit Mehta
  • 16,278
  • 11
  • 50
  • 64
2

Use this it works for ios 6 and 7:

- (void)searchBarTextDidBeginEditing:(UISearchBar *)search
{
    UITextField *searchBarTextField = nil;
    for (UIView *mainview in search.subviews) {
        if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
            if ([mainview isKindOfClass:[UITextField class]]) {
                searchBarTextField = (UITextField *)mainview;
                break;
            }
        }
        for (UIView *subview in mainview.subviews) {
            if ([subview isKindOfClass:[UITextField class]]) {
                searchBarTextField = (UITextField *)subview;
                break;
            }
        }
    }
    searchBarTextField.enablesReturnKeyAutomatically = NO;
}
Ollie
  • 207
  • 4
  • 13
2

If you're looking for a bit more elegant solution, you could use recursion to find the textfield within the searchbar, as shown below. This should work for ios 6, 7, or any other future ios barring any deprecations by apple.

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    UITextField* textField = [self findTextFieldInView:searchBar];
    if (textField) {
        textField.enablesReturnKeyAutomatically = NO;
    }
}

-(UITextField*)findTextFieldInView:(UIView*)view {
    if ([view isKindOfClass:[UITextField class]]) {
        return (UITextField*)view;
    }
    for (UIView* subview in view.subviews) {
        UITextField* textField = [self findTextFieldInView:subview];
        if (textField) {
            return textField;
        }
    }
    return nil;
}
ufmike
  • 93
  • 8
2

Simple Swift version:

if let searchTextField:UITextField = searchBar.subviews[0].subviews[2] as? UITextField {
                searchTextField.enablesReturnKeyAutomatically = false
}
odemolliens
  • 2,581
  • 2
  • 32
  • 34
1

Here is a solution using Swift. Just paste it in your viewDidLoad function and make sure that you have an IBOutlet of your searchBar in the code. (on my example below, the inputSearchBar variable is the IBOutlet)

   // making the search button available when the search text is empty
    var searchBarTextField : UITextField!
    for view1 in inputSearchBar.subviews {
        for view2 in view1.subviews {
            if view2.isKindOfClass(UITextField) {
                searchBarTextField = view2 as UITextField
                searchBarTextField.enablesReturnKeyAutomatically = false
                break
            }
        }
    }
rsc
  • 10,348
  • 5
  • 39
  • 36
0

In swift use UISearchBarDelegate

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    searchBar.enablesReturnKeyAutomatically = false  
}`

enter image description here

Kunal Nayek
  • 101
  • 2
  • 5