Ios 搜索栏从iphone上的搜索按钮展开
当我点击搜索按钮时,如何让我的搜索栏在导航栏中展开,就像推特应用程序中的那个按钮一样?现在我的搜索栏就在导航栏的顶部。这是我在单击导航栏中的搜索按钮时调用的函数:Ios 搜索栏从iphone上的搜索按钮展开,ios,iphone,swift,Ios,Iphone,Swift,当我点击搜索按钮时,如何让我的搜索栏在导航栏中展开,就像推特应用程序中的那个按钮一样?现在我的搜索栏就在导航栏的顶部。这是我在单击导航栏中的搜索按钮时调用的函数: func searchbuttonclicked(sender: UIBarButtonItem) { var searchresults = storyboard?.instantiateViewControllerWithIdentifier("searchresults") as searchResultsControl
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
进行子类化,使其使用导航控制器中的搜索栏,而不是默认情况下显示的搜索栏