轻按UISearchBar时,取消按钮将滑入,并且TextField位于上方以容纳该按钮。我将其称为“取消按钮动画”。

在我的应用程序中,当用户点击“搜索”按钮时,带有UISearchControllerUITableView的 View 会淡入。我希望该 View 以“cancel-button-animation”已经结束的方式淡入。与iOS音乐应用类似。我已经试过了:

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)

当我将searchBar第一响应者设置为屏幕之外时,是否应该完成取消按钮动画的完成?没有。

现在,取消按钮动画发生在屏幕上,然后 View (TableView)淡入。如何在 View 的Alpha值为0时使取消按钮动画发生?

最佳答案

这是UICatalog sample code的Apple的Swift版本。

就像“音乐”和“日历”一样,它会将搜索 Controller 设置为动画,使其位于导航栏上方。

从该项目可以看到,在动画过程中,可以根据需要看到searchBar取消按钮。

当用户点击您的搜索按钮时,只需调用类似的代码即可,如本示例的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)
    }
}

10-06 07:30
查看更多