I am trying to add a button in front of a GMSMapView
view.
Here is my code:
@IBOutlet weak var testButton: UIButton!
@IBOutlet weak var mapView2: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
loadView()
}
then here is the loadView()
method
override func loadView() {
let camera = GMSCameraPosition.camera(withLatitude: 54.2785691, longitude: -8.4620978, zoom: 18.0)
let mapView2 = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
view = mapView2
mapView2.bringSubviewToFront(testButton)
}
Here is the error that I am getting:
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
Any help would be appreciated!
You can try
class MyVc:UIViewController {
@IBOutlet weak var testButton: UIButton!
@IBOutlet weak var mapView2: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.camera(withLatitude: 54.2785691, longitude: -8.4620978, zoom: 18.0)
mapView2.animate(to: camera)
view.bringSubviewToFront(testButton)
}
}
I expect UI in IB
View->
-testButton
-mapView2
Btw instead of bringSubviewToFront
you can re-arrange the views to
View->
-mapView2
-testButton
Where bottom element is at the front
Also don't implement loadView
for such issue as it only matters when you create the vc programmatically or want to add some item in addition to IB and in latter case you should call super.loadView()