3

I need some help with animiting (sliding in) a search bar using swift, I've tried looking up tutorials etc but I can't seem to find anything. A visual representation would be something like this:

When I click the magnifying glass icon on the right of the navigation bar, I want the search bar to slide in from the left. All the tutorials that I found were about creating search bars under navigation bars or static ones etc.

AtulParmar
  • 4,358
  • 1
  • 24
  • 45
Misbah Ali
  • 31
  • 1
  • 7

3 Answers3

0

You can check out navigationItem.titleView to place the search bar directly on the navigation bar, but implementing the animation you desire will be a very tricky. Also take a look at this question which contains some tips regarding your question.

cocavo
  • 163
  • 8
0

First i declare the search bar:

var searchBar: UISearchBar!

on viewDidLoad() function i initiated the searchbar as follows:

self.searchBar = UISearchBar(frame: CGRect(x:300, y:0, width:300, height:20))

When the magnifying glass is clicked, i run the following code:

UIView.animate(withDuration: 0.25) {
    self.searchBar.frame = CGRect(x:0, y:0, width:300, height:20)
}

Hope this helps!

Daniel Miranda
  • 371
  • 2
  • 5
0

You could implement UISearchBarDelegate and play with some kind of alpha or size animations like this:

class MyViewController: UIViewController, UISearchBarDelegate {
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {

        UIView.animate(withDuration: 0.6, animations: { /*animate here*/ }, completion:  nil)
    }
}
blacktiago
  • 351
  • 2
  • 11