我在视图中显示地图。我正在使用auto layout,这就是为什么我在viewDidLayoutSubviews()中调用create map view函数。viewDidLayoutSubviews()调用了两次,并创建了两次映射,但未从视图中删除初始映射。当我在viewDidLoad()中调用create map view函数时,它只创建了一次,而且不适合视图框架。
我的代码是。。。

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    print("viewDidLayoutSubviews")

    loadMapView()//Call map view function

    let width = mapSubView.frame.size.width
    let x = mapSubView.frame.minX
    let y = mapSubView.frame.minY

    searchBar.frame = CGRect(x: x+10, y: y+10, width: width, height: 40)
    mapSubView.addSubview(searchBar)
    searchBar.delegate = self
    // hide cancel button
    searchBar.showsCancelButton = true
    // set Default bar status.
    searchBar.searchBarStyle = UISearchBarStyle.default

    let y1 = searchBar.frame.maxY
    searchTableView.frame = CGRect(x: x, y: y1, width: width, height: searchTableView.frame.size.height)
    mapSubView.addSubview(searchTableView)

    searchTableView.delegate = self
    searchTableView.dataSource = self


}

//Create map view
func loadMapView() {
    // Create a GMSCameraPosition that tells the map to display the
    // coordinate -33.86,151.20 at zoom level 6.
    let camera = GMSCameraPosition.camera(withLatitude: 19.3822559, longitude: 80.2194394, zoom: 6.0)
    let mapView = GMSMapView.map(withFrame: CGRect(x: 1, y: 1, width: mapSubView.frame.size.width-2, height: mapSubView.frame.size.height-2), camera: camera)
    mapSubView.addSubview(mapView)
     print("map : \(mapSubView.frame.size.height)")
    print("map : \(mapView)")
    // Creates a marker in the center of the map.
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: 19.3822559, longitude: 80.2194394)
    marker.title = ""
    marker.snippet = ""

    marker.map = mapView
}

如何在我的mapSubView中拟合地图?
ios - 当我们在viewDidLayoutSubviews()中调用 map  View 功能时, map  View 显示两次?-LMLPHP

最佳答案

在once变量中嵌入代码

var once = true

//
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    print("viewDidLayoutSubviews")
    if once {
        loadMapView()//Call map view function

        let width = mapSubView.frame.size.width
        let x = mapSubView.frame.minX
        let y = mapSubView.frame.minY

        searchBar.frame = CGRect(x: x+10, y: y+10, width: width, height: 40)
        mapSubView.addSubview(searchBar)
        searchBar.delegate = self
        // hide cancel button
        searchBar.showsCancelButton = true
        // set Default bar status.
        searchBar.searchBarStyle = UISearchBarStyle.default

        let y1 = searchBar.frame.maxY
        searchTableView.frame = CGRect(x: x, y: y1, width: width, height: searchTableView.frame.size.height)
        mapSubView.addSubview(searchTableView)

        searchTableView.delegate = self
        searchTableView.dataSource = self
        once = false
    }
}

或在viewDidLoad中使用约束
mapView.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate( [

    mapView.leadingAnchor.constraint(equalTo: mapSubView.leadingAnchor, constant: 0),
    mapView.trailingAnchor.constraint(equalTo: mapSubView.trailingAnchor, constant: 0),
    mapView.topAnchor.constraint(equalTo: mapSubView.topAnchor, constant: 0),
    mapView.bottomAnchor.constraint(equalTo: mapSubView.bottomAnchor, constant: 0),

])

关于ios - 当我们在viewDidLayoutSubviews()中调用 map View 功能时, map View 显示两次?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51300838/

10-10 13:26