当我们在viewDidLayoutSubviews()中调用地图视图功能时,地图视图显示两次?

时间:2018-07-12 08:33:52

标签: ios swift google-maps ios-autolayout nsautolayout

我正在视图中显示地图。我正在使用自动布局,这就是为什么我在viewDidLayoutSubviews()中调用create map view function的原因。 viewDidLayoutSubviews()调用了两次,创建了两次地图,但是没有从视图中移除初始地图。当我在viewDidLoad()中调用create map view function时,它仅创建一次,并且不适合视图框架。

我的代码是...

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中放置地图?

enter image description here

1 个答案:

答案 0 :(得分:1)

将代码嵌入一次var

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),

])