I haven’t encountered the search function before, so I haven’t done it yet. When I used it in this project, I checked the data. Because our project supports iOS8 or higher devices, I used UISearchController, and then I used it happily. There’s no navigation, no sectionIndex, no potholes at all, it’s amazing, but there are tons of potholes waiting for me. Here’s how I used it and the pitfalls I encountered. #UISearchController is used using lazy loading:

- (UISearchController *)searchController {
    if(! _searchController) {UISearchController *searchVC = [[UISearchController alloc] initWithSearchResultsController:nil];
        _searchController = searchVC;
        // Unmask
        _searchController.dimsBackgroundDuringPresentation = NO;
        _searchController.searchResultsUpdater = self;
        _searchController.searchBar.placeholder = @ "search";
        // Change the searchBar text
        [_searchController.searchBar setValue:@ "cancel" forKey:@"_cancelButtonText"];
        // Change the cancel button font color
        self.searchController.searchBar.tintColor = [UIColor colorWithRed:239 / 255.0 green:128 / 255.0 blue:25 / 255.0 alpha:1];
        / / remove the searchController. Upper and lower border of the searchBar (black line)
        UIImageView *barImageView = [[[_searchController.searchBar.subviews firstObject] subviews] firstObject];
        barImageView.layer.borderColor = UIColorFromRGB(0xe5e5e5).CGColor;
        barImageView.layer.borderWidth = 1;
        // Change the searchController background color
        _searchController.searchBar.barTintColor = UIColorFromRGB(0xe5e5e5);
         // This is an important step to display searbar on the interface (searbar sometimes disappears when added in the tableView header view, the reason is not found)
        [self.view addSubview:_searchController.searchBar];
        
    }
    return _searchController;
}
Copy the code

Add the searchBar to the tableView’s headerView, and then add the sectionIndex to the tableView. Because sectionIndex takes up space, the tableView moves to the left as a whole, and searchBar is no exception. See the picture below:

    [self.brandListTableView setSectionIndexBackgroundColor:[UIColor clearColor]];
Copy the code

The solution is to add searchBar to a UIView, and UIView to tableHeaderView. At the same time, the sectionIndex background color should be cleared. That’s the code above (because sectionIndex floats on top), and it solves the problem perfectly.

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0, _brandListTableView.bounds.size.width, _searchController.searchBar.size.height)];
    [headerView addSubview:self.searchController.searchBar];
    / / set tableHeaderView
    [self.brandListTableView setTableHeaderView:headerView];
Copy the code

Effect:

    self.definesPresentationContext = YES;
Copy the code

The explanation given is:

Set definesPresentationContext to YES, can guarantee in UISearchController under active users to push on to the next view controller after the search bar will not remain in interface. Know where you want UISearchController to be displayed At the time of setting hidesNavigationBarDuringPresentation this property to YES, can lead to a search box to enter edit mode, the searchbar invisible, migration – 64; When set to NO, enter edit mode input will lead to the height is 64 ious, speculation is the navigation bar NO renders b, if add the above line of code, at the time of setting hidesNavigationBarDuringPresentation this property to YES, Input box into edit mode normal display and use; When set to NO, the search box entered the edit mode, resulting in a downward offset of 64. The specific reason has not been found yet

After the above problem is solved, a new problem arises, that is, after the search results, the tableView will be offset 20px up. As shown in figure:

- (void)viewDidLayoutSubviews {
        if(self.searchController.active) {
              [self.brandListTableView setFrame:CGRectMake(0.20, TLScreenWidth, self.view.height - 20)];
        }else {
              self.brandListTableView.frame =self.view.bounds; }}Copy the code

After settlement:

      self.automaticallyAdjustsScrollViewInsets = NO;
Copy the code

The above is the UISearchController I used in this project and the pit I encountered. Welcome to exchange information. If there is anything wrong, please give me more advice!