Make UISearchBar dismiss like Music App

狂风中的少年 提交于 2020-01-12 09:14:57

问题


Notice how the searchBar magnifying glass and placeholder text both shift over on dismissal:

Is there any way I could dismiss this search bar without the text and icon moving over? See the animation of the Music app for the animation I am trying to achieve.

I have a UISeachBar that gets presented when the search button is clicked in a nav bar.

@IBAction func searchButtonPressed(sender: AnyObject) {

    searchController = UISearchController(searchResultsController: nil)
    searchController.searchResultsUpdater = self
    searchController.searchBar.delegate = self
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.dimsBackgroundDuringPresentation = false
    presentViewController(searchController, animated: true, completion: nil)
}

Now when the cancel button is pressed, I want the whole view to move up without seeing the SearchBar icon and placeholder text slide to the center of the searchBar as the view moves up off screen.

I know there is the method searchBarCancelButtonClicked(searchBar:), but this seems to automatically dismiss the SearchBar and I don't know how to control that.

How can I make the SearchBar dismiss like the one in the Music app?

EDIT: Here is the only delegate method I am using:

func updateSearchResultsForSearchController(searchController: UISearchController) {
    masterFilter.removeAll()
    filterContentForSearchText(searchController.searchBar.text!)
    tableView.reloadData()
}

回答1:


This is how I have achieved this. It is simple and easy.

extension UIViewController {


    func setNavigationBarItem() {

        let searchaButton = UIButton.init(frame: CGRectMake(0, 0, 30, 30))
        searchaButton.setBackgroundImage(UIImage(named: "search.png"), forState: UIControlState.Normal)
        searchaButton.addTarget(self, action: #selector(self.searchPressed), forControlEvents: UIControlEvents.TouchUpInside)


        let rightButton: UIBarButtonItem = UIBarButtonItem.init(customView: searchaButton)

        navigationItem.rightBarButtonItem = rightButton

    }


    public func searchPressed(){

        var searchController: UISearchController!

        // Create the search results view controller and use it for the UISearchController.
        let searchResultsController = storyboard!.instantiateViewControllerWithIdentifier("SearchResultController") as! SearchResultsViewController

        // Create the search controller and make it perform the results updating.
        searchController = UISearchController(searchResultsController: searchResultsController)
        searchController.searchResultsUpdater = searchResultsController
        searchController.hidesNavigationBarDuringPresentation = false

        // Present the view controller.
        presentViewController(searchController, animated: true, completion: nil)

    }

}

Note:

This code is for old swift version.

I hope this will help you.




回答2:


The searchBar expands to the right because the search is being cancelled, and the cancel button is disappearing.

Instead of canceling the search and triggering that animation, simply explicitly dismiss the search controller.

You can do this by setting

searchController.active = false



回答3:


To make this animation work, you'll need to understand custom UIViewController transitions. Within the UISearchBarDelegate method -searchBarCancelButtonClicked(searchBar:), you'll need to do a few things:

  1. Hide the UINavigationBar on the UISearchController dismissal
  2. Animate the UITableView to the bottom of the frame and fade out.
  3. If you're presenting search modally, ensure that you're setting the modalPresentationStyle to UIModalPresentationCurrentContext.


来源:https://stackoverflow.com/questions/32287894/make-uisearchbar-dismiss-like-music-app

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