4
votes

When a UISearchBar is tapped, the cancel button slides in and the TextField lies over to accommodate the button. I will call this the cancel-button-animation.

In my app, when the user taps the search button, a view with a UISearchController and a UITableView fades in. I want the view to fade in with the cancel-button-animation already finished. Similar to the iOS music app. I have tried this:

self.myTableView.frame = CGRectMake(0, 20, self.view.bounds.width, self.view.bounds.height - 20)
self.resultSearchController.searchBar.becomeFirstResponder()

UIView.animateWithDuration(0.3, delay: 0.0, options: .CurveLinear, animations: {
    self.myTableView.alpha = CGFloat(1.0)

        }, completion: nil)

When I make the searchBar first responder off screen, shouldn't that complete the cancel-button-animation finish? It doesn't.

Right now, the cancel-button-animation happens while on screen, THEN the view (TableView) fades in. How can I make the cancel-button-animation happen while the view's alpha is at 0?

1
You can present a UISearchController which will slide down in place, with its cancel button already visible. Calendar does this, as well as Music.user4151918
I am having trouble figuring out how to apply this Objective-C code to my UISearchBar implementation.MortalMan

1 Answers

7
votes

Here's Apple's Swift version from UICatalog sample code.

It animates a search controller into place over the navigation bar, exactly like Music and Calendar do.

As you can see from that project, the searchBar cancel button is already visible, during the animation, as you desired.

When the user taps your search button, just call similar code, as illustrated in by this sample's IBAction.

/*
    Copyright (C) 2015 Apple Inc. All Rights Reserved.
    See LICENSE.txt for this sample’s licensing information

    Abstract:
    A view controller that demonstrates how to present a search controller over a navigation bar.
*/

import UIKit

class SearchPresentOverNavigationBarViewController: SearchControllerBaseViewController {
    // MARK: Properties

    // `searchController` is set when the search button is clicked.
    var searchController: UISearchController!

    // MARK: Actions

    @IBAction func searchButtonClicked(button: UIBarButtonItem) {
        // Create the search results view controller and use it for the UISearchController.
        let searchResultsController = storyboard!.instantiateViewControllerWithIdentifier(SearchResultsViewController.StoryboardConstants.identifier) 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)
    }
}