Cannot set searchBar as firstResponder

前端 未结 21 1865
别跟我提以往
别跟我提以往 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:17

    Swift 5

    class ViewController {
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            // Only after viewDidAppear searchController can be activated
            searchController?.isActive = true
        }
    }
    
    extension ViewController: UISearchControllerDelegate {
        // didPresentSearchController not work for me
        func presentSearchController(_ searchController: UISearchController) {
            searchController.searchBar.becomeFirstResponder()
        }
    }
    

提交回复
热议问题