How to open UISearchBar like iPhone settings searc

2019-07-21 21:38发布

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

2条回答
对你真心纯属浪费
2楼-- · 2019-07-21 21:58

This works on iOS 11

navigationItem.hidesSearchBarWhenScrolling = true

And use navigationItem.searchController = searchController

Documentation

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

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-07-21 22:06

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):

enter image description here

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

enter image description here

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))
查看更多
登录 后发表回答