UISearchBar presented by UISearchController in table header view animates too far when active

后端 未结 15 1090
长情又很酷
长情又很酷 2020-11-29 01:28

I am using UISearchController to present a search bar inside the header view of a tableview:

...
self.searchController.hidesNavigationBarDuringPresentation =         


        
15条回答
  •  天命终不由人
    2020-11-29 01:54

    My turn. I also had this problem when I followed the WWDC sample code.

    I noticed that if I didn't set the scopeButtonTitles property of the searchBar, the searchBar would not be visible. Upon closer inspection, it just had a frame of CGRectZero. This means that setting the scopeButtonTitles sets the frame behind the scenes. So If don't want to show any scopeButtonTitles, but still want to not have to hardcode UISearchBar to a specific height, set the scopeButtonTitles to an empty array.

    self.searchController = UISearchController(searchResultsController: nil)
    self.searchController.searchResultsUpdater = self
    self.searchController.searchBar.scopeButtonTitles = []
    self.tableView.tableHeaderView = self.searchController.searchBar
    

    Setting the scopeButtonTitles to an array of 1 strings will not display the scope button titles, but still have logic to deal with the view, essentially screwing up the layout.

    Props to Apple's QA team (applicable to iOS 8)

提交回复
热议问题