我有一个UISearchController
内心UINavigationBar
.用户必须点击a UIBarButtonItem
,然后我实例化一个新的UIViewController
然后呈现它,以便开始搜索.
class ViewController: UIViewController { var searchController: UISearchController! override func viewDidLoad() { super.viewDidLoad() setupSearchController() } func setupSearchController() { searchController = UISearchController(searchResultsController: nil) searchController.searchBar.delegate = self searchController.searchResultsUpdater = self searchController.searchBar.showsCancelButton = true searchController.dimsBackgroundDuringPresentation = false searchController.hidesNavigationBarDuringPresentation = false definesPresentationContext = true navigationItem.titleView = searchController.searchBar } }
我事先做了大量研究,但仍无法找到解决方案......
帮助使搜索控制器成为第一个响应者将非常感激.
UISearchBar
在主线程上制作第一个响应者是解决方案.
override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) DispatchQueue.main.async { self.searchController.searchBar.becomeFirstResponder() } }