将 UISearchController 与 UINavigationController 一起使用

Using UISearchController with UINavigationController

我遇到了与 相同的问题,但没有任何答案。 (将 searchController.active 设置为 false 会清除我不想要的搜索文本。)我想使用 UISearchController,以便用户可以搜索我的 UITableView 中的项目列表。我在 Interface Builder 中连接了一个 show segue,它在选择 table 视图的单元格时触发。 问题是,如果用户搜索某些内容然后单击 table 的单元格,当它转到新视图时,搜索栏就在那里。理想情况下,我希望在用户搜索时导航栏在搜索栏​​旁边 "replaced",然后在用户单击单元格时导航栏 return,然后搜索栏出现当用户单击 "Back" 按钮时返回。 (这是现在已弃用的 UISearchDisplayController 的工作方式。)我怎样才能做到这一点?这是我的 table 视图的控制器。

class ItemSearchViewController: UITableViewController, UISearchResultsUpdating
{
    var searchController: UISearchController?

    let itemList = [ItemList(category:"Chocolate", name:"chocolate Bar", price: 1234),
    ItemList(category:"Chocolate", name:"chocolate Chip", price: 1234),
    ItemList(category:"Chocolate", name:"dark chocolate", price: 1234),
    ItemList(category:"Hard", name:"lollipop", price: 1234),
    ItemList(category:"Hard", name:"candy cane", price: 1234),
    ItemList(category:"Hard", name:"jaw breaker", price: 1234),
    ItemList(category:"Other", name:"caramel", price: 1234),
    ItemList(category:"Other", name:"sour chew", price: 1234),
    ItemList(category:"Other", name:"gummi bear", price: 1234)]

    var filteredList : [ItemList] = []

    override func viewDidLoad()
    {
        super.viewDidLoad()

        self.title = "Item Search"
        self.tableView.delegate = self
        self.tableView.dataSource = self

        self.searchController = UISearchController(searchResultsController: nil)
        self.searchController!.searchResultsUpdater = self
        self.searchController!.hidesNavigationBarDuringPresentation = true
        self.searchController!.dimsBackgroundDuringPresentation = false
        self.searchController!.searchBar.searchBarStyle = .Minimal
        self.searchController!.searchBar.sizeToFit()
        self.tableView.tableHeaderView = self.searchController!.searchBar  
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
    {
        if (segue.identifier == "itemDetail")
        {
            let itemDetailViewController = segue.destinationViewController as UIViewController
            let selectedCell = sender as UITableViewCell
            itemDetailViewController.title = selectedCell.textLabel?.text
        }
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
        var item : ItemList

        if self.searchController!.active
        {
            item = self.filteredList[indexPath.row]
        }
        else
        {
            item = self.itemList[indexPath.row]
        }

        cell.textLabel!.text = item.name
        return cell
}

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        if (self.searchController!.active)
        {
            return self.filteredList.count
        }
        else
        {
            return self.itemList.count
        }
    }
}

extension ItemSearchViewController: UISearchResultsUpdating
{
    func updateSearchResultsForSearchController(searchController: UISearchController)
    {
        if (searchController.searchBar.text.isEmpty)
        {
            self.filteredList = self.itemList
        }
        else
        {
            let searchPredicate =
            {
                (item: ItemList) -> Bool in
                item.name.rangeOfString(searchController.searchBar.text, options: .CaseInsensitiveSearch) != nil
            }
            self.filteredList = self.itemList.filter(searchPredicate)
        }
        self.tableView.reloadData()
    }
}

viewDidLoad()

中添加这一行
definesPresentationContext = true

来自 definesPresentationContext

的文档

A Boolean value that indicates whether this view controller's view is covered when the view controller or one of its descendants presents a view controller.

讨论

When a view controller is presented, iOS starts with the presenting view controller and asks it if it wants to provide the presentation context. If the presenting view controller does not provide a context, then iOS asks the presenting view controller's parent view controller. iOS searches up through the view controller hierarchy until a view controller provides a presentation context. If no view controller offers to provide a context, the window's root view controller provides the presentation context.

If a view controller returns true, then it provides a presentation context. The portion of the window covered by the view controller's view determines the size of the presented view controller's view. The default value for this property is false.