Swift: Search bar created at Auto Focus

前端 未结 3 1622
灰色年华
灰色年华 2021-01-14 11:34

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 t

相关标签:
3条回答
  • 2021-01-14 11:44

    try this

     @IBOutlet weak var searchBar: UISearchBar!
    override func viewDidLoad() 
    {
            super.viewDidLoad()
    searchBar.becomeFirstResponder()
    }
    
    0 讨论(0)
  • 2021-01-14 11:44

    Swift 5.2

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        searchController.isActive = true
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        searchController.delegate = self
    }
    
    extension ViewController: UISearchControllerDelegate {
        func didPresentSearchController(_ searchController: UISearchController) {
            searchController.searchBar.becomeFirstResponder()
        }
    }
    
    0 讨论(0)
  • 2021-01-14 11:58

    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()
       }
    }
    
    0 讨论(0)
提交回复
热议问题