Swift: Search bar created at Auto Focus

孤人 提交于 2019-12-01 09:38:54

问题


I am creating a table view and a search bar by clicking on a button.But I need the search bar to appear at Auto Focus ( where the user enters text immediately with no need to click inside the search bar). How can I do that ?


回答1:


try this

 @IBOutlet weak var searchBar: UISearchBar!
override func viewDidLoad() 
{
        super.viewDidLoad()
searchBar.becomeFirstResponder()
}



回答2:


This should do it.

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    searchController.active = true
}

...

extension GDSearchTableViewController: UISearchControllerDelegate {
   func didPresentSearchController(searchController: UISearchController) {
      searchController.searchBar.becomeFirstResponder()
   }
}


来源:https://stackoverflow.com/questions/31786050/swift-search-bar-created-at-auto-focus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!