您好,我希望searchBar始终可见,这就是我所拥有的:

searchController = UISearchController(searchResultsController: nil)
tableView.tableHeaderView = searchController.searchBar
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false

有办法做到这一点吗?

提前致谢

最佳答案

关于什么



override func viewDidAppear(_ animated: Bool) {
   super.viewDidAppear(animated)
   searchController.searchBar.becomeFirstResponder()
}



看一下苹果的例子(源代码):https://developer.apple.com/library/content/samplecode/TableSearch_UISearchController/Introduction/Intro.html

注意:从iOS 11开始,您应该使用UINavigationController而不是表格标题 View :

        if #available(iOS 11.0, *) {
          // For iOS 11 and later, we place the search bar in the navigation bar.
          navigationController?.navigationBar.prefersLargeTitles = true
          navigationItem.searchController = searchController
          // We want the search bar visible all the time.
          navigationItem.hidesSearchBarWhenScrolling = false
      } else {
          // For iOS 10 and earlier, we place the search bar in the table view's header.
          tableView.tableHeaderView = searchController.searchBar
      }

关于swift - 搜索栏始终可见,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34723301/

10-12 01:52