微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐