How can I programmatically force a search in the UISearchBar?

后端 未结 8 1231
遥遥无期
遥遥无期 2021-02-05 05:15

How can I force the UISearchBar to automatically start a new search (like pressing the Search button)? Is there an easy way to achieve this?

相关标签:
8条回答
  • 2021-02-05 06:15

    I found that I needed to set the UISearchViewController to active before changing the value of the search field programatically to make UISearchViewController automatically perform the search:

    // Need to set the searchDisplayController to active to 
    // make the changes in search text trigger a search.
    self.searchDisplayController.active = YES;
    self.searchDisplayController.searchBar.text = @"New search string";
    

    Swift 4

    self.searchController.isActive = true
    self.searchController.searchBar.text = "New search string"
    
    0 讨论(0)
  • 2021-02-05 06:17

    seems dumb, but works, so it's not dumb ;)

    NSString * forceReload = self.searchDisplayController.searchBar.text;
    self.searchDisplayController.searchBar.text = forceReload;
    
    0 讨论(0)
提交回复
热议问题