2014-11-04 76 views
1

當我單擊搜索按鈕(如Twitter應用程序中的搜索按鈕)時,如何讓我的搜索欄在導航欄中展開?現在我的搜索欄只是在導航欄的頂部。這是我打電話的時候,我在導航欄中點擊搜索按鈕的功能:搜索欄從iphone上的搜索按鈕擴展

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) 
} 

回答

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以使其使用導航控制器中的搜索欄,而不是默認顯示的搜索欄。

+0

謝謝你的幫助 – jk3455 2014-11-04 06:59:13