推送下一个视图控制器时延迟删除 UISearchBar

Delay in UISearchBar being removed when pushing next view controller

Xcode10.1,Swift4.2

ViewController A 正在运行 LargeTitlesUISearchControllerUITableView

在选择 TableViewCell 后按下 ViewController B(也是一个 TableView)时,space 被移除的 UISearchBar 占用会出现延迟,如动画所示。

ViewController B 也有一个 UISearchController,但按照通常的行为,它是隐藏的,直到用户在屏幕上向下拖动。

有谁知道如何防止这种情况发生?

附加信息和代码:

ViewController一个 当点击单元格时,它会使用 Show(例如 Push) 在故事板上选择 Animates segue 来推送 ViewController B。使用 prepareForSegue 方法注入数据。

performSegue(withIdentifier: "showLogbook", sender: self)

ViewController乙:

class LogbookVC: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchResultsUpdating {

    //Extract
    private let searchController = UISearchController(searchResultsController: nil)


    @IBOutlet private var addButton: UIBarButtonItem!
    @IBOutlet private var shareButton: UIBarButtonItem!
    @IBOutlet private var backButton: UIBarButtonItem!
    @IBOutlet private var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = 75.0
        //loading tableView data
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.navigationBar.prefersLargeTitles = true
    self.definesPresentationContext = true
    configureSearchController()
    fetchAndSortLogEntries()
}

private func configureSearchController() {
    navigationItem.searchController = searchController
    searchController.searchResultsUpdater = self
    searchController.searchBar.scopeButtonTitles = ["Newest", "Oldest"]
    searchController.searchBar.delegate = self
    searchController.searchBar.barStyle = .black
    searchController.searchBar.tintColor = K.Colors.appMid
    searchController.searchBar.keyboardAppearance = .dark
    searchController.searchBar.returnKeyType = .done
    searchController.dimsBackgroundDuringPresentation = false
    let attributes = [NSAttributedString.Key.foregroundColor: K.Colors.appMid]
    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)
    searchController.searchBar.setValue("Reset", forKey: "_cancelButtonText")
    let searchBarCancelButton = searchController.searchBar.value(forKey: "cancelButton") as? UIButton
    searchBarCancelButton?.setTitle("Reset", for: .normal)
}

我认为这是使用 LargeTitles 并且在相邻的 ViewControllers 中有两个 UISearchControllers 时的错误。

我发现通过将 UISearchController 添加到 viewDidAppear 中的 NavigationItem 可以解决问题,但是必须异步添加,否则会出现以下致命错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Only one palette with a top boundary edge can be active outside of a transition.

此代码需要添加到两个 ViewControllers,以便在导航回 ViewController A 时也可以防止问题行为。

Xcode10.1,Swift4.2

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        DispatchQueue.main.async {
            self.navigationItem.searchController = self.searchController
            self.searchController.searchResultsUpdater = self
            self.searchController.searchBar.delegate = self
        }
    }

此代码将确保 NavigationBar 在下一个 ViewController 出现时不被隐藏:

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.isNavigationBarHidden = false
    }