Problem description

On iOS 11 or later, when you click the input box to search, or click cancel without searching, the search box cannot bounce back correctly and is covered under the navigation bar.

The problem code

   override func viewDidLoad() {super.viewDidLoad() // Configure the search controller uiSearchController = uiSearchController (searchResultsController: nil) uiSearchController.searchResultsUpdater = self uiSearchController.searchBar.placeholder ="Please search by name"
        uiSearchController.searchBar.searchBarStyle = .minimal
        uiSearchController.searchBar.sizeToFit()
        uiSearchController.searchBar.returnKeyType = .done
        uiSearchController.hidesNavigationBarDuringPresentation = false
        uiSearchController.dimsBackgroundDuringPresentation = false
        uiSearchController.searchBar.enablesReturnKeyAutomatically = false
        
        if # available (iOS 11.0. *) {
            navigationItem.searchController = uiSearchController
            navigationItem.hidesSearchBarWhenScrolling = false
        }else{
            self.tableView.tableHeaderView = uiSearchController.searchBar
            self.automaticallyAdjustsScrollViewInsets = false}}Copy the code

The correct code

   override func viewDidLoad() {super.viewDidLoad() // Configure the search controller uiSearchController = uiSearchController (searchResultsController: nil) uiSearchController.searchResultsUpdater = self uiSearchController.searchBar.placeholder ="Please search by name"
        uiSearchController.searchBar.searchBarStyle = .minimal
        uiSearchController.searchBar.sizeToFit()
        uiSearchController.searchBar.returnKeyType = .done
        uiSearchController.hidesNavigationBarDuringPresentation = false
        uiSearchController.dimsBackgroundDuringPresentation = false
        uiSearchController.searchBar.enablesReturnKeyAutomatically = false
        
        if # available (iOS 11.0. *) {
            navigationItem.searchController = uiSearchController
            navigationItem.hidesSearchBarWhenScrolling = false
        }else{
            let uiView:UIView = UIView(frame: CGRect(x: 0,y: 0,width: UIScreen.main.bounds.width,height: 45))
            uiView.addSubview(uiSearchController.searchBar)
            self.tableView.tableHeaderView = uiView
            self.automaticallyAdjustsScrollViewInsets = false}}Copy the code

This problem is also present in the official sample download and may not have been fixed;

The specific principle of correction is not clear, but a layer of VIEW is wrapped outside.