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

时间:2019-02-19 13:03:21

标签: ios swift xcode uisearchbar uisearchcontroller

Xcode 10.1,Swift 4.2

ViewController A具有 LargeTitles UISearchController UITableView

在选择TableViewCell后推动ViewController B(也就是TableView)时,如动画所示,UISearchBar所占用的空间存在延迟。

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

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

enter image description here

其他信息和代码:

ViewController A 轻按单元格时,它会使用情节提要中的 Show(例如Push)选择来推送ViewController B,并选择 Animates 。使用 prepareForSegue 方法注入数据。

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

ViewController B:

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)
}

1 个答案:

答案 0 :(得分:0)

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

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

*** 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时也可以防止出现问题。

Xcode 10.1,Swift 4.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
    }

enter image description here