Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SearchBar disappears from headerview in iOS 7

Tags:

I have a tableview for showing a list of devices in my application. When viewWillAppear is called, I add the self.searchDisplayController.searchBar as a subview to a headerView. I then assign self.tableView.tableHeaderView = headerView. It looks like this: enter image description here

I scroll the tableview down so that headerview goes out of view and then go to some other view controller by tapping on a cell. When I come back to this tableView, scroll up to the headerView, the searchBar becomes invisible, however on tapping the invisible area the searchDisplayController gets activated and the cancel button doesn't work. This happens for iOS 7 only. Why is this happening? enter image description here
Note: It happens only if the headerView is out of the view when I come back to the tableViewController.

like image 208
tech savvy Avatar asked Sep 27 '13 06:09

tech savvy


1 Answers

I've just had the same issue. When I go to debug into the delegate method of UISearchDisplayController at the end search state, the searchBar becomes a subview of an UIView, not the UITableView. Please see below code:

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {     //My Solution: remove the searchBar away from current super view,     //then add it as subview again to the tableView     UISearchBar *searchBar = controller.searchBar;     UIView *superView = searchBar.superview;     if (![superView isKindOfClass:[UITableView class]]) {         NSLog(@"Error here");         [searchBar removeFromSuperview];         [self.tableView addSubview:searchBar];     }     NSLog(@"%@", NSStringFromClass([superView class])); } 

My solution is remove the searchBar away from current super view, then add it as subview again to the tableView. I've already tested successfully. Hope that help!

Regards

like image 159
Phien Tram Avatar answered Oct 11 '22 11:10

Phien Tram