How to open UISearchBar like iPhone settings search bar?

﹥>﹥吖頭↗ 提交于 2019-12-24 00:46:50

问题


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



来源:https://stackoverflow.com/questions/47987619/how-to-open-uisearchbar-like-iphone-settings-search-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!