在iOS 8中的导航栏中显示搜索栏 [英] Displaying search bar in navigation bar in iOS 8

查看:163
本文介绍了在iOS 8中的导航栏中显示搜索栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UISearchDisplayController 有一个 boolean 属性名为 displaysSearchBarInNavigationBar 。在iOS 8中,我的搜索栏向上移动了什么?非常感谢任何指导。

UISearchDisplayController had a boolean property called displaysSearchBarInNavigationBar. What's the equivalent in iOS 8 to have my search bar move up there? Any guidance is greatly appreciated.

这是我的代码,我不完全确定为什么这不起作用。当我单击搜索栏时,它会消失而不是移动自身和导航栏。

Here's my code, I'm not entirely sure why this isn't working. When I click the search bar, it just disappears instead of moving itself and the navigation bar up.

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的说法:

According to Apple :


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

UISearchDisplayController is deprecated in iOS 8. (Note that UISearchDisplayDelegate is also deprecated.) To manage the presentation of a search bar and display search results in iOS 8 and later, instead use UISearchController.

UISearchController 类定义了一个界面,用于管理与搜索结果控制器内容一致的搜索栏的显示。搜索结果控制器, UIViewController 对象/index.html#//apple_ref/occ/instp/UISearchController/searchResultsControllerrel =noreferrer> searchResultsController 属性,管理搜索结果。

The UISearchController class defines an interface that manages the presentation of a search bar in concert with the search results controller’s content. The search results controller, a UIViewController object specified by the searchResultsController property, manages the results of the search.

现在您可以使用 UISearchController 来显示导航中的搜索栏bar以下列方式:

Now you can use the UISearchController to show the search bar in your navigation bar in the following way:

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 就像在这个故事板中一样:

But you have to consider you need to set a UINavigationController like in this Storyboard :

您可以轻松选择 ViewController 并查看以下步骤:

You can do it very easy just select your ViewController and see the following steps:

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

And then you should see in your device when you make click in the search bar the following picture:

我希望这个帮助你

这篇关于在iOS 8中的导航栏中显示搜索栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆