How to open UISearchBar like iPhone settings searc

2019-07-21 21:25发布

问题:

Is there any easy way to handle UISearchBar position similar to iPhone settings search bar? Because iPhone settings search bar is opening while pull down the screen and open animation controlled by our pull action.

I want to do the same in my app. Help me if any easy way you know.

Thanks

回答1:

I think the easiest way to achieve this animation is:

It would be the default behavior for showing/hiding the search bar when adding the search bar inside the scroll view (table view) on top of the cell(s):

By applying the above, scrolling to down would let the search bar to be hidden (collapsed) and vise versa:

I assume that there is nothing to do more.

Also

if you want to let the search bar to be hidden by default, you could achieve it -as a workaround- by scrolling the table view to the top (you might want to call this in the viewDidLoad()):

let searchBarHeight = searchBar.frame.size.height
tableView.setContentOffset(CGPoint(x: 0, y: searchBarHeight), animated: false))


回答2:

This works on iOS 11

navigationItem.hidesSearchBarWhenScrolling = true

And use navigationItem.searchController = searchController

Documentation

https://developer.apple.com/documentation/uikit/uinavigationitem/2897305-searchcontroller