1

In my project i need an UISearchBar to scroll on an UITableView. So i just put it on an UITableViewCell like this:

    searchBar                   = [[SearchBar alloc] initWithFrame:cell.bounds];
    searchBar.delegate          = self;
    searchBar.placeholder       = NSLocalizedString(@"Search friends", "");
    searchBar.layer.borderWidth = 1.f;
    searchBar.layer.borderColor = [UIColor whiteColor].CGColor;

    sdc = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:nil];
    sdc.delegate = self;

    [cell addSubview:searchBar];

and it works fine on iOS 6. But on iOS 7 it switches to another superview (and literally disappears from the screen) when just getting in focus. At first i thought it's something wrong with my project which causes such a behaviour but then i created a simple test project and i verified this - indeed, it's a rule, on iOS 7.1 UISearchBar added to UITableViewCell moves to another superview and gets lost from the screen right after becoming first responder.

I have overridden willMoveToSuperview: method on the search bar and i see that it moves from UITableViewCellScrollView to a view which class is UIView and whose superview's class is UISearchDisplayControllerContainerView.

After few hours of search and experiments i'm not able to figure out what causes this behaviour and how to escape it. All i know for sure is that it happens right between calls to these two UISearchDisplayDelegate methods: searchDisplayControllerWillBeginSearch: and searchDisplayControllerDidBeginSearch:.

Did anyone ever encountered this? Any ideas how to resolve it?

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
  • 1
    Instead of using a cell for the search bar, make the search bar the table view's `headerView`. – rmaddy Apr 28 '14 at 15:54
  • possible duplicate of [SearchBar disappears from headerview in iOS 7](http://stackoverflow.com/questions/19044156/searchbar-disappears-from-headerview-in-ios-7) – jrc Aug 18 '14 at 15:35

1 Answers1

1

Are you sure you need this bizarre way to add UISearchBar to UITableViewCell to just scroll it? I simply use smthng like this:

UISearchBar *mainSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
mainSearchBar.delegate = self;

UISearchDisplayController *searchCon = [[UISearchDisplayController alloc] initWithSearchBar:mainSearchBar contentsController:self ];
searchCon.delegate = self;
searchCon.searchResultsDataSource = self;
searchCon.searchResultsDelegate = self;

mainTableView.tableHeaderView = mainSearchBar;
Sergey Grischyov
  • 11,995
  • 20
  • 81
  • 120