本文介绍了搜索栏始终可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hello I want the searchBar always visible, that is what I have:

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

有没有办法做到这一点?

Is there way to accomplish this?

提前致谢

推荐答案

怎么样

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

?

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

Have a look at Apple's example (source code): https://developer.apple.com/library/content/samplecode/TableSearch_UISearchController/Introduction/Intro.html

注意 - 从 iOS 11 开始,您应该使用 UINavigationController 而不是表头视图:

NB - from iOS 11 onwards you should use UINavigationController instead of the table header 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
      }

这篇关于搜索栏始终可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-04 23:07