iOS 11 search bar jumping to top of screen

前端 未结 5 1988
青春惊慌失措
青春惊慌失措 2020-12-09 19:07

I\'ve got a UITableView in which I set its header to be a search bar.

tableView.tableHeaderView = searchController.searchBar

Everything wor

5条回答
  •  感动是毒
    2020-12-09 19:26

    My first post on SO, i hope i am doing this right.

    I had the exact same problem and really struggled with finding a solution. But now i have managed to fix it by subclassing UISearchController.

    This subclass has two properties:

    var customDelegate : CustomSearchControllerDelegate!
    var searchBarWithCustomSize : UISearchBar!
    

    So instead of using the searchBar of UISearchController i can now use the new property searchBarWithCustomSize from my custom searchController, like this:

    tableView.tableHeaderView = customSearchController.searchBarWithCustomSize
    

    This makes the searchbar behave correctly when active. You also have more freedom with this searchBarWithCustomSize, for example you can change its frame if needed and so on.

    In my implementation i need the delegate to know when the searchbar textfield has been changed but i guess the usage of a delegate is dependent on your situation.

提交回复
热议问题