搜索栏从iphone上的搜索按钮扩展
问题描述:
当我单击搜索按钮(如Twitter应用程序中的搜索按钮)时,如何让我的搜索栏在导航栏中展开?现在我的搜索栏只是在导航栏的顶部。这是我打电话的时候,我在导航栏中点击搜索按钮的功能:搜索栏从iphone上的搜索按钮扩展
func searchbuttonclicked(sender: UIBarButtonItem) {
var searchresults = storyboard?.instantiateViewControllerWithIdentifier("searchresults") as searchResultsController
var searchcontroller = UISearchController(searchResultsController: searchresults)
searchcontroller.hidesNavigationBarDuringPresentation = false
searchcontroller.searchResultsUpdater = searchresults
searchcontroller.modalTransitionStyle = .CrossDissolve
presentViewController(searchcontroller, animated: true, completion: nil)
}
答
从this answer,您可以使用类似:
// declare as property
var searchBar: UISearchBar!
// in viewDidLoad
searchBar = UISearchBar(frame: CGRectMake(0.0, -80.0, 320.0, 44.0))
navigationController?.navigationBar.addSubview(searchBar)
// in action to show
searchBar.frame = CGRectMake(0.0, 0, 320, 44)
// in viewWillDisappear
searchBar.removeFromSuperview()
然而,searchBar
财产上UISearchController
是只读,因此您需要继承UISearchController
以使其使用导航控制器中的搜索栏,而不是默认显示的搜索栏。
谢谢你的帮助 – jk3455 2014-11-04 06:59:13