Wyświetlanie paska wyszukiwania na pasku nawigacji w systemie iOS 8
UISearchDisplayController
posiadał boolean
nieruchomość o nazwie displaysSearchBarInNavigationBar
. Jaki jest odpowiednik w iOS 8, aby mój pasek wyszukiwania przeniósł się tam? Wszelkie wskazówki są bardzo mile widziane.
Oto Mój kod, nie jestem do końca pewien, dlaczego to nie działa. Kiedy klikam pasek wyszukiwania, po prostu znika zamiast przesuwać się i pasek nawigacji w górę.
import UIKit
class ViewController: UIViewController, UISearchResultsUpdating, UISearchControllerDelegate, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource {
let searchController = UISearchController(searchResultsController: nil)
var tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.searchController.searchResultsUpdater = self
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.hidesNavigationBarDuringPresentation = true
self.searchController.dimsBackgroundDuringPresentation = true
tableView.dataSource = self
tableView.delegate = self
self.navigationItem.titleView = searchController.searchBar
self.definesPresentationContext = true
self.setupSearchBar()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
}
func setupSearchBar() {
// Set search bar position and dimensions
var searchBarFrame: CGRect = self.searchController.searchBar.frame
var viewFrame = self.view.frame
self.searchController.searchBar.frame = CGRectMake(searchBarFrame.origin.x, searchBarFrame.origin.y + 64,viewFrame.size.width, 44)
// Add search controller's search bar to our view and bring it to forefront
self.view.addSubview(self.searchController.searchBar)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell()
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
}
1 answers
Według Apple:
UISearchDisplayController
jest przestarzały w iOS 8. (Zauważ, żeUISearchDisplayDelegate
jest również przestarzały.) Aby zarządzać prezentacją paska wyszukiwania i wyświetlać wyniki wyszukiwania w systemie iOS 8 i nowszym, użyjUISearchController
.Klasa
UISearchController
definiuje interfejs, który zarządza prezentacją paska wyszukiwania w zgodzie z treścią kontrolera wyników wyszukiwania. Kontroler wyników wyszukiwania, obiektUIViewController
określony przez właściwość searchResultsController zarządza wynikami wyszukiwania.
Teraz możesz użyć UISearchController
, aby wyświetlić pasek wyszukiwania w pasku nawigacyjnym w następujący sposób:
class ViewController: UIViewController, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {
var searchController : UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
self.searchController = UISearchController(searchResultsController: nil)
self.searchController.searchResultsUpdater = self
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = true
self.navigationItem.titleView = searchController.searchBar
self.definesPresentationContext = true
}
func updateSearchResults(for searchController: UISearchController) {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Ale musisz wziąć pod uwagę, że musisz ustawić UINavigationController
jak w tym Storyboardzie:
Możesz to zrobić bardzo łatwo po prostu wybierz swój ViewController
i zobacz następujące kroki:
A następnie powinieneś zobaczyć w swoim urządzeniu, gdy wykonasz kliknięcie w pasek wyszukiwania na poniższym obrazku:
Mam nadzieję, że to ci pomoże
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-24 18:09:43