I managed to solve a very similar problem to the one you're experiencing by programmatically setting the UISearchController
up like so:
Note ~ The error persists if hidesNavigationBarDuringPresentation
is not set to NO.
#pragma mark - View Life Cycle
- (void)viewDidLoad {
[super viewDidLoad];
self.results = [[NSArray alloc] init];
// 1 \\ Results Table View
UITableView *searchResultsTableView = [[UITableView alloc] initWithFrame:self.tableView.frame];
[searchResultsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:Identifier];
searchResultsTableView.delegate = self;
searchResultsTableView.dataSource = self;
// 2 \\ init search results table view & setting its table view
self.searchResultsTableViewController = [[UITableViewController alloc] init];
self.searchResultsTableViewController.tableView = searchResultsTableView;
self.searchResultsTableViewController.view.backgroundColor = [UIColor blackColor];
// 3 \\ init a search controller with it's tableview controller for results
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchResultsTableViewController];
self.searchController.hidesNavigationBarDuringPresentation = NO; // √
self.searchController.searchResultsUpdater = self;
self.searchController.delegate = self;
self.searchController.searchBar.barTintColor = [UIColor blackColor];
// 4 \\ Make an appropriate search bar (size, color, attributes) and add it as the header
[self.searchController.searchBar sizeToFit];
self.tableView.tableHeaderView = self.searchController.searchBar;
self.tableView.tableHeaderView.backgroundColor = [UIColor blackColor];
// 5 \\ Enable presentation context
self.definesPresentationContext = YES;
self.tableView.backgroundColor = [UIColor clearColor];
self.currentUser = [PFUser currentUser];
}
Added Further Details:
- Interface Builder: UIViewController (not UITableViewController)
Add a UITableView
with Constraints:
a. 
b. *My Nav Bar is custom height - your "Top Space to: Top Layout Guide" may be changed
Add the Delegate & Data Source outlets from the added Table View to the UIViewController
a. Don't forget to do so programmatically:
@interface AddUsersViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
In your Interface Extension:
a. Set your UISearchConroller
's Delegate and Data Source:
#import "AddUsersViewController.h"
#define ResultsTableView self.searchResultsTableViewController.tableView
#define Identifier @"Cell"
@interface AddUsersViewController () <UISearchControllerDelegate, UISearchResultsUpdating>
@property (nonatomic) UISearchController *searchController;
@property (nonatomic) UITableViewController *searchResultsTableViewController;
@property (nonatomic, weak) UIBarButtonItem *leftBarButton;
@property (nonatomic) NSArray *results;
@end
Although this worked for me, I have discovered that there are other possible scenarios that could be causing this. My "issue" wasn't that the search bar moved down 20px (or so) but that it moved up. I attributed this to a customized UINavigationBar header that included an image title and custom NavBarButtons. With that in mind, here are two more possible solutions:
.
Original Scenarios:
Your Container View's extendedLayoutIncludesOpaqueBars
Property
a. Storyboard: Check √ "Under Opaque Bars" for the table views in storyboard.
b. Programmatically: yourTableView(s).extendedLayoutIncludesOpaqueBars
= YES;
.
Set scopeButtonTitles
to an empty array. (below)
self.searchController = UISearchController(searchResultsController: nil);
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.scopeButtonTitles = [];
self.tableView.tableHeaderView = self.searchController.searchBar;
The later two answers come from THIS question and may prove to be beneficial in checking out too.
Thanks to BananaNeil's research and testing, he discovered that if you follow the my recommendations and set your searchController.searchBar.y = 0
your search bar should be exactly where you were hoping it would be. √