Seems like a common iOS bug but I've tried every solution I can find and have seen no results.
I am creating a UISearchController
and adding it to a UIView
wrapper like this:
self.searchController = [[SearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.delegate = self;
[self.searchBarWrapper addSubview:self.searchController.searchBar];
[self.searchController.searchBar sizeToFit];
The problem is that whenever the search bar is touched, the keyboard pops up, but the whole search bar disappears.
I looked at these posts:
- My UISearchBar in UISearchController disappear when I start searching. Why?
- iOS 9 searchBar disappears from table header view when UISearchController is active
- UISearchController searchBar disappears on first click
- Disappearing UISearchController in a TableViewController that is in a UINavigationController
- UITableView disappears when UISearchController is active and a new tab is selected
- Search Bar disappears after tap on it
- UISearchController searchBar in tableHeaderView animating out of the screen
- search bar getting disappeared in ios UIsearchcontroller
- Search Bar disappeared from view while typing
and tried every solution, ultimately adding this initialization code:
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.definesPresentationContext = NO;
self.navigationController.definesPresentationContext = YES;
self.navigationController.extendedLayoutIncludesOpaqueBars = YES;
self.extendedLayoutIncludesOpaqueBars = YES;
and these delegate functions
- (void)willPresentSearchController:(UISearchController *)searchController {
// definitely runs, if I put a breakpoint here it stops
self.navigationController.navigationBar.translucent = YES;
[self.searchBarWrapper addSubview:self.searchController.searchBar];
searchController.searchResultsController.view.hidden = NO;
}
-(void)willDismissSearchController:(UISearchController *)searchController{
self.navigationController.navigationBar.translucent = NO;
searchController.searchResultsController.view.hidden = NO;
}
But the search bar still disappears when touched. What else can I try?
I think the difference between my code and the rest of the posts is that I'm using a UIViewController
created programmatically without a XIB. There is a UITableView
child view, but it doesn't interact directly with the search bar. There is no UINavigationBar
, the search bar is at the top of the view.
UPDATE:
This is my view hierarchy relative to the search bar:
- UIViewController
- UIView
- UIScrollView
- UIViewController
- UIView
- UISearchBar