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

后端 未结 15 1024
长情又很酷
长情又很酷 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)

    0 讨论(0)
  • 2020-11-29 01:58

    Ok so I finally figured out a solution for myself. Though it's more than likely we have other things in our code / storyboard (that's why this is a hard question to answer), for the most part I followed Apple's tutorial on UISearchController: (https://developer.apple.com/library/ios/samplecode/TableSearch_UISearchController/Introduction/Intro.html)

    Just about all my code is identical to theirs, but I couldn't get the search bar to not jump when clicking inside of it. So what I did was check "under opaque bars" for both the original table view and the search results table view in my storyboard. That got the search bar to stop jumping.

    But there was one last issue, which was the fact that the first row of the results table view was hidden by the search bar. In order to fix that I added self.tableView.contentInset = UIEdgeInsetsMake(kHeightOfTableViewCells, 0, 0, 0); to the viewDidLoad method of my results table view. Voila!

    0 讨论(0)
  • 2020-11-29 01:58

    Mine was the order of elements on the main controller .xib file.

    This worked.

    This didn't work.

    0 讨论(0)
  • 2020-11-29 02:00

    Found it! This seems to be the offensive line in viewDidLoad of the presenting vc:

    self.edgesForExtendedLayout = UIRectEdgeNone;
    

    Removed that and the search bar stayed in place as predicted.

    0 讨论(0)
  • 2020-11-29 02:04

    definesPresentationContext = true

    override func viewDidLoad() {
            super.viewDidLoad()
    
            searchController = UISearchController(searchResultsController: nil)
            searchController.searchResultsUpdater = self
            searchController.hidesNavigationBarDuringPresentation = false
    
            searchController.dimsBackgroundDuringPresentation = true
            searchController.searchBar.searchBarStyle = UISearchBarStyle.Prominent
            self.tableView.tableHeaderView = searchController.searchBar
    
            definesPresentationContext = true
    
    0 讨论(0)
  • 2020-11-29 02:07

    I was able to prevent UISearchBar from shifting down by removing the following line from my ViewController: self.definesPresentationContext = YES;

    0 讨论(0)
提交回复
热议问题