以编程方式更改 Google 地图大小 (Swift)
Change Google Map Size Programmatically (Swift)
我正在尝试以编程方式添加 Google GMSMapView 但卡住了,希望得到任何帮助。我想将地图的约束设置为视图的边缘。
然而,当我这样做时,我得到了一个错误,"Unable to activate constraint with anchors and because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies?"
var mapView:GMSMapView?
override func viewDidLoad() {
super.viewDidLoad()
mapView = GMSMapView.map(withFrame: CGRect(x: 100, y: 100, width: 200, height: 200), camera: GMSCameraPosition.camera(withLatitude: 51.050657, longitude: 10.649514, zoom: 5.5))
mapView?.center = self.view.center
mapView?.topAnchor.constraint(equalTo: view.topAnchor).isActive=true
mapView?.leftAnchor.constraint(equalTo: view.leftAnchor).isActive=true
mapView?.rightAnchor.constraint(equalTo: view.rightAnchor).isActive=true
mapView?.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 60).isActive=true
self.view.addSubview(mapView!)
}
如果省略锚点,我可以让 Google 地图正确加载,所以我相信我已经正确设置了它。我只是不明白如何正确设置锚点的概念。
This solution 类似,但更侧重于添加具有自动布局的视图。
当您在将视图添加到父视图之前将约束应用于视图时,就会出现此问题。同时将 translatesAutoresizingMaskIntoConstraints 设置为 false 以使约束起作用。
self.view.addSubview(mapView!)
mapView?.translatesAutoresizingMaskIntoConstraints = false
mapView?.topAnchor.constraint(equalTo: view.topAnchor).isActive=true
mapView?.leftAnchor.constraint(equalTo: view.leftAnchor).isActive=true
mapView?.rightAnchor.constraint(equalTo: view.rightAnchor).isActive=true
mapView?.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 60).isActive=true
我正在尝试以编程方式添加 Google GMSMapView 但卡住了,希望得到任何帮助。我想将地图的约束设置为视图的边缘。
然而,当我这样做时,我得到了一个错误,"Unable to activate constraint with anchors and because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies?"
var mapView:GMSMapView?
override func viewDidLoad() {
super.viewDidLoad()
mapView = GMSMapView.map(withFrame: CGRect(x: 100, y: 100, width: 200, height: 200), camera: GMSCameraPosition.camera(withLatitude: 51.050657, longitude: 10.649514, zoom: 5.5))
mapView?.center = self.view.center
mapView?.topAnchor.constraint(equalTo: view.topAnchor).isActive=true
mapView?.leftAnchor.constraint(equalTo: view.leftAnchor).isActive=true
mapView?.rightAnchor.constraint(equalTo: view.rightAnchor).isActive=true
mapView?.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 60).isActive=true
self.view.addSubview(mapView!)
}
如果省略锚点,我可以让 Google 地图正确加载,所以我相信我已经正确设置了它。我只是不明白如何正确设置锚点的概念。
This solution 类似,但更侧重于添加具有自动布局的视图。
当您在将视图添加到父视图之前将约束应用于视图时,就会出现此问题。同时将 translatesAutoresizingMaskIntoConstraints 设置为 false 以使约束起作用。
self.view.addSubview(mapView!)
mapView?.translatesAutoresizingMaskIntoConstraints = false
mapView?.topAnchor.constraint(equalTo: view.topAnchor).isActive=true
mapView?.leftAnchor.constraint(equalTo: view.leftAnchor).isActive=true
mapView?.rightAnchor.constraint(equalTo: view.rightAnchor).isActive=true
mapView?.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 60).isActive=true