I'm displaying map in my view. I'm using auto layout, that's why i called create map view function in viewDidLayoutSubviews(). viewDidLayoutSubviews() Called two times and map created two times, but initial map not removed from the view. When i call create map view function in viewDidLoad() it's creating only one time and it's not fit in to the view frame.
My code is...
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
}
How to fit map in my mapSubView?
Embed the code inside 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
}
}
or use constraints in 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),
])