我在 viewDidLoad()
中配置了一个 UISearchController
如下:
let searchController = UISearchController(searchResultsController: nil);
searchController.delegate = self;
searchController.searchResultsUpdater = self;
searchController.searchBar.delegate = self;
self.searchController.isActive = true;
self.searchController.searchBar.becomeFirstResponder();
self.navigationItem.searchController = searchController;
尽管我已经设置了isActive
并将searchBar
设置为firstResponder
,但是当 View 出现在屏幕上时,UISearchController 并未处于事件状态。手动点击该字段可以正确打开它。
在 iOS 12 中,isActive
和 becomeFirstResponder
属性是否在 viewDidLoad
中不受尊重?
最佳答案
不确定那里发生了什么,但它在导航栏中是独一无二的。
不过这似乎可以解决问题。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
DispatchQueue.main.async(searchController.searchBar.becomeFirstResponder)
}
关于ios - 以编程方式显示导航栏的 UISearchController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54157674/