当我单击搜索按钮时,如何使我的搜索栏在导航栏中展开,就像推特应用程序中的搜索按钮一样?现在,我的搜索栏正好位于导航栏的顶部。这是我点击导航栏中的搜索按钮时调用的功能:
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)
}
根据这个答案,您可以使用以下内容:
// 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()
但是,UISearchController
上的searchBar
属性是只读的,因此您需要对UISearchController
进行子类化,使其使用导航控制器中的搜索栏,而不是默认情况下显示的搜索栏。