ios – 将UISearchController与UINavigationController一起使用

我遇到的问题与 here相同,没有任何答案. (将searchController.active设置为false会清除搜索文本,这是我不想要的.)我想使用UISearchController,以便用户可以搜索我的UITableView中的项目列表.我在Interface Builder中连接了一个show segue,它在选择表视图的单元格时触发.
 问题是,如果用户搜索某些内容然后单击该表格的单元格,当它转移到新视图时,搜索栏就会位于该位置.理想情况下,我希望在用户搜索时导航栏被搜索栏“替换”,然后在用户点击单元格时返回导航栏,然后在用户单击时返回搜索栏“返回键. (这是现在已弃用的UISearchdisplayController的工作方式.)如何实现这一目标?这是我的表视图的控制器.

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

    let itemList = [ItemList(category:"Chocolate",name:"chocolate Bar",price: 1234),ItemList(category:"Chocolate",name:"chocolate Chip",name:"dark chocolate",ItemList(category:"Hard",name:"lollipop",name:"candy cane",name:"jaw breaker",ItemList(category:"Other",name:"caramel",name:"sour chew",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.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...