Can't make table view inactive when search bar

2019-08-08 17:35发布

问题:

I have a table view with a search bar above it, and when the search bar is pressed I want the table view to become inactive. So I have this method:

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    self.tableView.userInteractionEnabled = NO;
}

with the same class being the UISearchBarDelegate. For some reason though it doesn't disable the table view. Any thoughts? Let me know if there's any other pieces of code needed to help. Thanks.

回答1:

Not sure why userInteractionEnabled is not making your UITableView inactive, but I can suggest an alternative approach.

I recently moved a project over from manually adding a UISearchBar as a subview of my UITableView to using a UISearchDisplayController. I can definitely recommend doing it this way. The SearchDisplayController will animate in a alpha blended view behind the search bar which prevents interaction with the underlying table view, which would avoid the problem you are having.

Update:

This might be something to do with the way you are adding your UISearchDisplayController. Does your view hierarchy look like this?