代码之家  ›  专栏  ›  技术社区  ›  Naresh

在viewDidLayoutSubviews()中调用map view函数时,map view会显示两次?

  •  0
  • Naresh  · 技术社区  · 7 年前

    我正在视图中显示地图。我使用的是自动布局,这就是为什么我在viewdidLayoutSubviews()中调用create map view函数的原因。viewDidLayoutSubviews()调用了两次,并创建了两次映射,但未从视图中删除初始映射。当我在viewdidload()中调用create map view函数时,它只创建了一次,不适合视图框架。

    我的代码是…。

    override func viewDidLayoutSubviews()。{
    super.viewdidlayoutSubviews()。
    打印(“ViewDidLayoutSubviews”)
    
    loadmapview()//调用map view函数
    
    让width=mapSubview.frame.size.width
    设x=mapsubview.frame.minx
    设y=mapSubView.frame.miny
    
    searchbar.frame=cgrect(x:x+10,y:y+10,width:width,height:40)
    mapsubview.addsubview(搜索栏)
    searchbar.delegate=本人
    //隐藏取消按钮
    searchbar.showscancelbutton=真
    //设置默认条形图状态。
    searchbar.searchbarstyle=uisearchbarstyle.default
    
    设y1=searchbar.frame.maxy
    searchTableView.frame=cDirect(x:x,y:y1,width:width,height:searchTableView.frame.size.height)
    mapSubview.addSubview(搜索表视图)
    
    searchTableView.delegate=自己
    searchTableView.datasource=自身
    
    
    }
    
    
    
    

    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
    }
    

    1 回复  |  直到 7 年前
        1
  •  1
  •   Hussain Chhatriwala    7 年前

    将代码嵌入once 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),
    
    ])
    
    推荐文章