单击 iOS10 时 UISearchBar 的位置向下移动

Position of UISearchBar moving down when clicked on iOS10

我只在 iOS 10 上遇到搜索栏问题(它在 iOS 11 上运行良好)而且我不知道发生了什么,我已经好几天了试图自己解决这个问题但没有成功,所以我希望这里有人能告诉我我做错了什么。 所以,最初,我的屏幕是这样的

当我点击搜索栏时,它是这样的

如果我键入文本,它会在搜索栏下方很好地显示结果。我只是不知道我的搜索栏和导航栏之间的 space 是从哪里来的。 我的搜索结果由另一个像这样声明的 viewcontroller 管理。

let locationSearchTable = storyboard!.instantiateViewController(withIdentifier: "LocationSearchTableViewController") as! LocationSearchTableViewController
    resultSearchController = UISearchController(searchResultsController: locationSearchTable)
    resultSearchController?.searchResultsUpdater = locationSearchTable

我的一些代码,尤其是我设置搜索栏的地方:

let searchBar = resultSearchController!.searchBar
        searchBar.sizeToFit()
        searchBar.placeholder = "Search for a place or address"
        if #available(iOS 11.0, *) {
            navigationItem.searchController = resultSearchController
            resultSearchController?.dimsBackgroundDuringPresentation = true
        } else {
            searchBar.delegate = self
            searchBar.searchBarStyle = .default
            searchBar.setShowsCancelButton(false, animated: true)
            searchBar.keyboardAppearance = .default
            navigationController?.navigationBar.isTranslucent = false
            searchBar.isTranslucent = false
            resultSearchController?.dimsBackgroundDuringPresentation = false
            mainView.addSubview(searchBar)
        }
        definesPresentationContext = true
        resultSearchController?.hidesNavigationBarDuringPresentation = false
    }

对于可能遇到相同问题的其他人,这是我现在的代码,它正在运行。

    let searchBar = resultSearchController!.searchBar
    searchBar.sizeToFit()
    searchBar.placeholder = "Search for a place or address"
    if #available(iOS 11.0, *) {
        navigationItem.searchController = resultSearchController
        resultSearchController?.hidesNavigationBarDuringPresentation = false
        resultSearchController?.dimsBackgroundDuringPresentation = true
    } else {
        searchBar.searchBarStyle = .default
        searchBar.setShowsCancelButton(false, animated: true)
        searchBar.keyboardAppearance = .default
        navigationController?.navigationBar.isTranslucent = false
        searchBar.barStyle = .default
        searchBar.isTranslucent = false
        resultSearchController?.hidesNavigationBarDuringPresentation = true
        resultSearchController?.dimsBackgroundDuringPresentation = false
        UI_mapView.addSubview(searchBar)
    }
    definesPresentationContext = true
    let currentLocationButtonItem = MKUserTrackingBarButtonItem(mapView: UI_mapView)
    self.navigationItem.rightBarButtonItem = currentLocationButtonItem