当我点击搜索按钮(如Twitter应用中的搜索按钮)时,如何在导航栏中展开我的搜索栏?现在我的搜索栏刚好落在导航栏的顶部。这是我单击导航栏中的搜索按钮时调用的函数:
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)
}
答案 0 :(得分:2)
从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
以使其使用导航控制器中的搜索栏而不是它默认呈现。