ios – UISearchController搜索栏无法获得焦点

在我的项目中,如果第二次将控制器(包含UISearchController)推送到导航堆栈,搜索栏似乎无法获得焦点.以下GIF重新编码演示了我在说什么:

As you can see, when search screen is pushed to the navigation stack the second time, I can't tap in the search bar

如您所见,当搜索屏幕第二次被推到导航堆栈时(通过点击放大镜),我无法再点击搜索栏.

重现问题的代码:如果您想查看代码,可以使用download the minimal project.

您需要设置UISearchControllerDelegate

    class SearchController: UITableViewController, UISearchControllerDelegate, UISearchBarDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()

        searchController = UISearchController(searchResultsController: nil)
        searchController.delegate = self
        searchController.searchBar.delegate = self

        self.definesPresentationContext = false
    }

在故事板中还需要将“显示推送”中的segue更改为“显示替换”

或使用扩展,但酷孩子现在正在这样做.

https://stackoverflow.com/questions/35013821/uisearchcontroller-search-bar-doesnt-get-focus

转载注明原文:ios – UISearchController搜索栏无法获得焦点