首页 > 解决方案 > 在 MapBox 层 iOS 上添加搜索栏

问题描述

我正在尝试在 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)
    }

标签: swiftmapbox

解决方案


@erikpartridge,这不是 mapbox 的问题,您需要为 UISearchBar 设置框架,如下所示,

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

推荐阅读