在iOS 8中的导航栏中显示搜索栏

Mih*_*ado 34 uisearchbar uisearchdisplaycontroller ios8

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
}
}
Run Code Online (Sandbox Code Playgroud)

Vic*_*ler 87

根据Apple的说法:

UISearchDisplayController在iOS 8中UISearchDisplayDelegate已弃用.(请注意,也已弃用.)要管理搜索栏的显示并在iOS 8及更高版本中显示搜索结果,请改用UISearchController.

UISearchController类定义了管理与搜索结果控制器的内容一致搜索栏的呈现的接口.搜索结果控制器UIViewControllersearchResultsController属性指定的对象,用于管理搜索结果.

现在,您可以使用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.
    }
}
Run Code Online (Sandbox Code Playgroud)

但是你必须考虑UINavigationController在这个Storyboard中设置一个类似的东西:

在此输入图像描述

您可以轻松选择ViewController并执行以下步骤:

在此输入图像描述

然后,当您在搜索栏中点击以下图片时,您应该会在设备中看到:

在此输入图像描述

我希望这对你有帮助