UISearchDisplayController
持っていたboolean
というプロパティをdisplaysSearchBarInNavigationBar
。iOS 8で、検索バーを上に移動するのと同じことは何ですか?どんなガイダンスでも大歓迎です。
これが私のコードです。なぜこれが機能しないのか完全にはわかりません。検索バーをクリックすると、検索バー自体とナビゲーションバーが上に移動するのではなく、消えてしまいます。
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
}
}
Appleによると:
UISearchDisplayController
iOS 8では非推奨です(UISearchDisplayDelegate
これも非推奨です。)iOS 8以降で検索バーの表示を管理し、検索結果を表示するには、代わりにを使用しますUISearchController
。この
UISearchController
クラスは、検索結果コントローラーのコンテンツと連携して検索バーの表示を管理するインターフェースを定義します。searchResultsControllerプロパティでUIViewController
指定されたオブジェクトである検索結果コントローラーは、検索結果を管理します。
これで、を使用しUISearchController
て、次の方法でナビゲーションバーに検索バーを表示できます。
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.
}
}
ただしUINavigationController
、このストーリーボードで「いいね」を設定する必要があることを考慮する必要があります。
を選択してViewController
次の手順を確認するだけで、非常に簡単に実行できます。
そして、検索バーをクリックすると、次の画像がデバイスに表示されます。
これがお役に立てば幸いです
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加