搜索栏从iphone上的“搜索”按钮展开

时间:2014-11-04 03:03:50

标签: ios iphone swift

当我点击搜索按钮(如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)
}

1 个答案:

答案 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以使其使用导航控制器中的搜索栏而不是它默认呈现。