在iOS 8中如何在导航栏中显示搜索栏

34

UISearchControllerUISearchDisplayController的替代品,iOS 8中使用它来在导航栏中显示搜索栏。将displaysSearchBarInNavigationBar属性设置为true即可实现此功能。

以下是我的代码,我不太确定为什么不起作用。当我点击搜索栏时,它只是消失了,而没有将自己和导航栏一起移动。

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个回答

88

根据苹果官方文档:

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

但是您需要考虑设置一个像这个Storyboard中的UINavigationController

enter image description here

您可以很容易地完成它,只需选择您的ViewController并查看以下步骤:

enter image description here

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

enter image description here

希望这能帮助您。


1
那对我来说并没有解决问题。我已经发布了我的代码,请随意查看并告诉我为什么它不按照我想要的方式运行。 - Mihado
1
@MihadAlzayat 我用一个导航栏测试了一下,它完美地运行并且不会消失,但你真正想要的是什么?是在表格视图的顶部,在导航栏下方放置一个搜索栏吗? - Victor Sigler
@MihadAlzayat 请查看更新的答案,了解如何按照您的要求使用导航栏来完成它。 - Victor Sigler
当然可以把搜索栏放在表格视图的标题栏中,如果你想的话。 - Victor Sigler
这行代码:self.navigationItem.titleView = searchController.searchBar 对我来说很好用。以下是我的全部代码:let searchController = UISearchController(searchResultsController: nil) searchController.dimsBackgroundDuringPresentation = false searchController.searchBar.delegate = self searchController.searchBar.placeholder = "搜索" searchController.searchBar.sizeToFit() searchController.hidesNavigationBarDuringPresentation = falseself.navigationItem.titleView = searchController.searchBar - xhinoda
显示剩余7条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接