Trouble implementing UISearchController

80 Views Asked by At

I am having trouble implementing a UISearchController

I have a UIViewController which contains a UITableView and a few buttons that are below of the table view.

- (void)viewDidLoad {

    [super viewDidLoad];
    ...
    _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    _searchController.searchResultsUpdater = self;
    _searchController.dimsBackgroundDuringPresentation = NO;
    _searchController.searchBar.scopeButtonTitles = @[];
    _searchController.searchBar.delegate = self;
    NSLog(@"tableViewHeader %@", NSStringFromCGRect(_tableView.tableHeaderView.frame));
    NSLog(@"searchBar       %@", NSStringFromCGRect(_searchController.searchBar.frame));
    _tableView.tableHeaderView = _searchController.searchBar;
    ...
}

NSLog returns:

tableViewHeader {{0, 0}, {0, 0}}
searchBar       {{0, 0}, {320, 44}}

In general I can't seem to get the tableViewHeader to be visible. Since I am not using Storyboards, I instantiate the UITableView as follows:

_tableView = [[UITableView alloc] init];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.separatorInset = UIEdgeInsetsZero;
_tableView.layoutMargins = UIEdgeInsetsZero;
_tableView.backgroundColor = [UIElementsStyleKit tableCellBackground];

[self.view addSubview:_tableView];

What could be going wrong?

Thanks

//EDIT: Just in case it was not implicit, the UITableView's dimensions are defined by NSLayoutConstraints and it shows perfectly. The problem is just that the UISearchBar does not get displayed.

1

There are 1 best solutions below

0
On BEST ANSWER

The solution to my problem was that I defined the UISearchController after having added the UITableView as a subview to self.view.

As soon as I moved the code before the [self.view addSubview:_tableView] everything worked as expected.