SearchBar未显示在导航栏上

时间:2018-10-17 17:45:41

标签: ios swift ios12 apple-developer

我试图在导航栏上显示搜索栏。

搜索栏应向我显示搜索结果,然后向我显示地图导航详细信息。

我在另一个项目中尝试了相同的代码,但似乎可以正常工作。

这种情况发生在最新版本的Xcode 10和iOS 12上。

代码如下:

// Protocol for dropping a pin at a specified place
protocol HandleMapSearch: class {
    func dropPinZoomIn(_ placemark:MKPlacemark)
}


class MapViewController: UIViewController {

    @IBOutlet weak var mapView: MKMapView!

    @IBAction func navigateButton(_ sender: Any) {

    }

    let locationManager = CLLocationManager()
    var currentLocation: CLLocation?
    var routeCoordinates = [CLLocationCoordinate2D]()

    var resultSearchController: UISearchController!

    var selectedPin: MKPlacemark?


    override func viewDidLoad() {
        super.viewDidLoad()

        mapView.delegate = self
        mapView.showsUserLocation = true

        let coordinateSpan = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
        guard let userCoordinate = locationManager.location?.coordinate else { return }
        let userRegion: MKCoordinateRegion = MKCoordinateRegion(center: userCoordinate, span: coordinateSpan)
        // Zoom to user location
        mapView.setRegion(userRegion, animated: true)

        // Hide back button
        self.navigationItem.setHidesBackButton(true, animated: false)

        let locationSearchTable = storyboard!.instantiateViewController(withIdentifier: "LocationSearchTable") as! LocationSearchTable
        resultSearchController = UISearchController(searchResultsController: locationSearchTable)
        resultSearchController.searchResultsUpdater = locationSearchTable
        let searchBar = resultSearchController!.searchBar
        searchBar.sizeToFit()
        searchBar.placeholder = "Search for places"
        navigationItem.titleView = resultSearchController?.searchBar
        resultSearchController.hidesNavigationBarDuringPresentation = false
        resultSearchController.dimsBackgroundDuringPresentation = true
        definesPresentationContext = true
        locationSearchTable.mapView = mapView
        locationSearchTable.handleMapSearchDelegate = self
    }

我没有错误,也没有控制台日志消息

0 个答案:

没有答案