移除 UISearchController 后,NavigationItem 不会 return 到其原始大小

NavigationItem does not return to its original size after removing the UISearchController

Wenderlich 人的 Candy Search 克隆版。我在 show/hide 搜索栏中添加了一个图标。

但是,从 navigationItem 中删除搜索控制器后,导航栏不会 return 到其原始大小。

有谁知道如何做到这一点?我尝试了一些东西,但 none 成功了。

self.searchController.isActive = false 没有任何区别。

还有一件事,在这个沮丧的时刻,如果你现在点击一行来实例化一个细节控制器并返回,导航栏会恢复到正常高度!

谢谢!

见截图:

完整项目在这里:https://github.com/HerrDoktorBD/CVSearch

相关代码:

lazy var searchController: UISearchController = {

    let searchController = UISearchController(searchResultsController: nil)

    searchController.searchBar.delegate = self
    searchController.searchResultsUpdater = self
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "Search Candy"

    // scope bar
    searchController.searchBar.scopeButtonTitles = [
        "All",
        "Chocolate",
        "Hard",
        "Other"
    ]

    return searchController
}()
func showSearchBar(show: Bool) {

    let sb: UISearchBar = searchController.searchBar

    if show {
        //print("show searchbar")

        UIView.animate(withDuration: 0.3,
                       delay: 0.0,
                       options: .curveEaseOut,
                       animations: {

            if #available(iOS 13, *) {
                self.navigationItem.searchController = self.searchController
            }

        }, completion: { (status) in

            sb.becomeFirstResponder()
        })
    }
    else {
        //print("hide searchbar")

        UIView.animate(withDuration: 0.3,
                       delay: 0.0,
                       options: .curveEaseOut,
                       animations: {

            sb.resignFirstResponder()

        }, completion: { (status) in

            if #available(iOS 13, *) {
                self.navigationItem.searchController = nil
            }
        })
    }
}

改为更新导航视图的高度

navigationController?.view.setNeedsLayout() 
navigationController?.view.layoutIfNeeded() 

编码愉快!