UISearchContainerViewController:立即显示结果控制器

问题描述

我正在尝试在系统“日历”应用中提供类似于“位置选择器”的搜索控制器。请注意,其结果在演示过程中如何可见,而在解雇期间仍保持可见。

在我的情况下,容器的背景为空白,直到用户点击搜索栏,才会显示任何结果。我通过重写viewDidAppear并设置searchController.isActive = true解决这个问题,但这感觉很hacky,而且看起来还是很糟糕。较早地设置isActive(在init,viewDidLoad,viewWillApppear中)无效。

我还尝试设置showsSearchResultsController / automaticallyShowsSearchResultsController属性,但这并没有任何改变。

有人知道如何尽早显示搜索结果吗?

class LocationSearchViewController: UISearchContainerViewController {
    init() {
        let resultsViewController = LocationResultsTableViewController()
        let searchController = UISearchController(searchResultsController: resultsViewController)
        searchController.searchResultsUpdater = resultsViewController
        searchController.hidesNavigationBarDuringPresentation = false
    
        searchController.searchBar.sizetoFit()
    
        super.init(searchController: searchController)
        searchController.searchBar.delegate = self

        // handle selections
        resultsViewController.delegate = self

        // doesn't seem to do anything         
        definesPresentationContext = true
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        // this shows the results,but it's too late - I want it visible as the
        // container is presenting (like the location picker in Calendar)
        // it cannot be done in viewDidLoad or viewWillAppear
        searchController.isActive = true
        dispatchQueue.main.async { [weak self] in
            self?.searchController.searchBar.becomeFirstResponder()
        }
    }
}

extension LocationSearchViewController: LocationSearchDelegate {
    // I call this from the results controller when the user selects a search result
    func locationPicker(didSelect mapItem: MKMapItem) {
        // We need to do both or it leaves the search results visible.
        // Even with both calls,something is wrong: you can see the controllers being
        // animated away sequentially,not as a single animation.
        searchController.isActive = false
        dismiss(animated: true,completion: nil)
    }
}

结果控制器可防止在没有搜索查询时隐藏该控制器,但是只有在用户点击搜索栏(或上面的viewDidAppear hack)之后,该控制器才会被调用

extension LocationResultsTableViewController: UISearchResultsUpdating {
    func updateSearchResults(for searchController: UISearchController) {
        searchController.searchResultsController?.view.isHidden = false
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...