Search code examples
iosswiftsearchbar

Position of UISearchBar moving down when clicked on iOS10


I have a trouble with a search bar only on iOS 10 (it works well on iOS 11) and I don't know what's going on, it's been several days I'm trying to solve this by myself without success so I hope someone here could tell me what I do wrong. So, initially, my screen is like this

enter image description here

And when I click in the search bar, it goes like that

enter image description here If I type text, it well displays the results under the search bar. I just don't know where this space between my search bar and the navigation bar comes from. The results of my search is managed by an another viewcontroller declared like this.

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

A bit of my code, especially where I set up the search bar:

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
    }

Solution

  • For others who might encounter the same issue, here is my code now and it's working.

        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