Cannot set searchBar as firstResponder

前端 未结 21 1879
别跟我提以往
别跟我提以往 2020-11-30 01:33

I have a searchBar I\'m setting in a tableviewcontroller. i\'ve referenced this similar question UISearchBar cannot become first responder after UITableView did re-appear bu

21条回答
  •  星月不相逢
    2020-11-30 02:05

    i fixed this as follows:

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        searchController.isActive = true
        
    }
    
    func didPresentSearchController(_ searchController: UISearchController) {
    
        DispatchQueue.main.async {
            searchController.searchBar.searchTextField.becomeFirstResponder()
        }
        
    }
    

    What happens is as soon as the view appears you set your searchControllers 'isActive' property to true. This fires the delegate method called didPresentSearchController. when that fires it means that searchcontroller is visible and thus can become first responder.

提交回复
热议问题