Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 搜索栏从iphone上的搜索按钮展开_Ios_Iphone_Swift - Fatal编程技术网

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
进行子类化,使其使用导航控制器中的搜索栏,而不是默认情况下显示的搜索栏