[英]UISearchController with Large titles, UI misbehave on dismissing
I have a UITableViewController which has a UISearchController in the navigation bar.我有一个 UITableViewController,它在导航栏中有一个 UISearchController。
When I tap the search, pull down once and then press cancel, I see a weird UI behavior.当我点击搜索,下拉一次然后按取消时,我看到了一个奇怪的 UI 行为。
I can't find any particular reasons why this happens.我找不到发生这种情况的任何具体原因。 This issue is not reproduced every time.
这个问题不是每次都重现。 It happens more often when the navigation bar has some buttons, as you can see in the GIF above.
当导航栏有一些按钮时,它会更频繁地发生,正如您在上面的 GIF 中看到的那样。
When I go back to the first view controller (which does not have any navigation bar buttons) and repeat same steps, the issue is reproduced on rare occasions.当我 go 回到第一个视图 controller (没有任何导航栏按钮)并重复相同的步骤时,该问题在极少数情况下会重现。
This issue happens only when you pull down in the ViewController which has the search controller.仅当您在具有搜索 controller 的 ViewController 中下拉时才会出现此问题。 If you are using a results controller and pull down in the results controller, this issue is not reproduced.
如果您使用结果 controller 并下拉结果 controller,则不会重现此问题。
Below is the code used in the sample app.下面是示例应用程序中使用的代码。
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)
}
}
I tried to change the tableView's content offset using tableView.setContentOffet
in UISearchControllerDelegate methods willPresentSearchController
and willDismissSearchController
but the y position varies for each device, and it cannot be a universal fix.我尝试使用 UISearchControllerDelegate 方法
willPresentSearchController
和willDismissSearchController
中的tableView.setContentOffet
更改 tableView 的内容偏移量,但 y position 因每个设备而异,它不能是通用修复。
PS: This issue doesn't occur in most simulators. PS:大多数模拟器不会出现这个问题。 So kindly try the same code in your mobile.
因此,请在您的手机中尝试相同的代码。
This issue can be prevented from happening by setting这个问题可以通过设置来防止发生
searchController.obscuresBackgroundDuringPresentation = true
This means that user can not scroll the table view after tapping search.这意味着用户在点击搜索后无法滚动表格视图。 However, the user can scroll the results controller after it is presented.
但是,用户可以在 controller 出现后滚动结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.