Sunil Targe
Sunil Targe

Reputation: 7459

How to open UISearchBar like iPhone settings search bar?

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

Upvotes: 3

Views: 2772

Answers (2)

Ahmad F
Ahmad F

Reputation: 31645

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

Upvotes: 4

Vyacheslav
Vyacheslav

Reputation: 27211

This works on iOS 11

navigationItem.hidesSearchBarWhenScrolling = true

And use navigationItem.searchController = searchController

Documentation

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

Upvotes: 4

Related Questions