我有一个UITableViewController,它的导航栏里有一个UISearchController。
当我点击搜索,向下拉一次,然后按取消键,我看到一个奇怪的UI行为。
我找不到任何特别的原因,为什么会发生这种情况。这个问题并不是每次都会重现。当导航栏有一些按钮时,它发生得更频繁,正如你可以在上面的GIF中看到的那样。
当我回到第一个视图控制器(没有任何导航栏按钮)并重复同样的步骤时,这个问题在极少数情况下会重现。
这个问题只发生在你在有搜索控制器的ViewController中下拉时。如果你使用的是结果控制器,并在结果控制器中向下拉,这个问题就不会重现。
以下是示例应用程序中使用的代码。
import UIKit
class SearchViewController: UITableViewController {
lazy var resultsController = ResultsVC()
lazy var searchController = UISearchController(searchResultsController: resultsController)
override func viewDidLoad() {
super.viewDidLoad()
configureSearchController()
navigationItem.largeTitleDisplayMode = .always
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.title = "Search VC"
}
func configureSearchController() {
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = true
searchController.obscuresBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = true
searchController.searchResultsUpdater = resultsController
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
navigationController?.pushViewController(SearchViewController(), animated: true)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 50
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "Searchable cell"
return cell
}
}
class ResultsVC: UITableViewController {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 50
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "Result cell"
return cell
}
}
extension ResultsVC: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
print(searchController.searchBar.text)
}
}
我试着改变tableView的内容偏移量,使用 tableView.setContentOffet
在UISearchControllerDelegate方法中。willPresentSearchController
和 willDismissSearchController
但每个设备的y位置都不一样,不能通用修复。
PS:这个问题在大多数模拟器中不会出现。所以请在你的手机中尝试同样的代码。
这个问题可以通过设置以下功能来防止发生
searchController.obscuresBackgroundDuringPresentation = true
这意味着用户在点击搜索后不能滚动表格视图。然而,用户可以在结果控制器呈现后滚动它。