点击UISearchBar
时,取消按钮会滑入,而TextField会翻转以容纳按钮。我将这称为取消按钮动画。
在我的应用中,当用户点按搜索按钮时,UISearchController
和UITableView
的视图会淡入。我希望视图淡入,取消按钮动画已经完成。与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第一响应者关闭时,不应该完成取消按钮动画结束吗?它没有。
现在,取消按钮动画在屏幕上发生,然后视图(TableView
)淡入。如何在取消按钮动画发生时,视图的alpha是在0?
答案 0 :(得分:7)
这是来自UICatalog sample code的Apple的Swift版本。
它将搜索控制器设置到导航栏上的位置,就像音乐和日历一样。
正如您从该项目中看到的那样,在动画期间,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)
}
}