在MapBox图层iOS上添加搜索栏

时间:2018-07-27 15:30:06

标签: swift mapbox

我正在尝试将搜索栏添加到iOS上MapBox的导航地图中,但是遇到了一些麻烦。在这种情况下,我试图以编程方式进行操作,因为我在故事板上没有很好的处理能力。我也尝试过弄乱zIndex,但仍然没有显示搜索栏。

这是到目前为止我得到的代码:

class ViewController: UIViewController, UISearchBarDelegate, MGLMapViewDelegate {
    var mapView: NavigationMapView!
    lazy var directions: DirectionsManager = DirectionsManager()
    var searchBar: UISearchBar!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Setup mapbox
        let styleURL = URL(string: "style")
        mapView = NavigationMapView(frame: view.bounds, styleURL: styleURL)

        view.addSubview(mapView)
        mapView.delegate = self
        mapView.showsUserLocation = true
        mapView.setUserTrackingMode(.follow, animated: true)


        mapView.localizeLabels()

        self.searchBar = UISearchBar()
        mapView.addSubview(searchBar)
    }

1 个答案:

答案 0 :(得分:1)

@erikpartridge ,这不是mapbox的问题,您需要像下面那样为UISearchBar设置框架,

    self.searchBar = UISearchBar(frame: CGRect(x: 15, y: 50, width: (view.bounds.width-30), height: 50))
    mapView.addSubview(searchBar)