How to add a button on the Google maps in iOS?

后端 未结 5 2084
北海茫月
北海茫月 2021-02-05 16:44

I am new to iOS Programming and I have downloaded the google maps sdk for iOS and followed the instruction on their website ( as shown in this link https://developers.google.com

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-05 17:26

    This problem occur because you try to add the map view with the code, earlier you add the button with the help of interface builder. When you add the mapView it would add at the top of the view that's why your button is not shown.

    You have two options for doing this.

    First approach

    • Drag and drop the UIButton from the interface builder and set the constraints accordingly

    • Then create the outlet of that button in the corresponding viewController

      @IBOutlet weak var bottonToAdd: UIButton!

    • After adding the map view to the view paste the blow code to bring the button at the top mapViewForScreen.addSubview(self.bottonToAdd) mapViewForScreen.bringSubviewToFront(self.bottonToAdd)

    After the above three step you can find out that you hav the button over mapview

    Second approach

    If you don't want to use the interface builder you can use the below code for the same.

        var bottonToAdd:UIButton = UIButton()
        bottonToAdd.setTitle("Button To Add", for: .normal)
        bottonToAdd.sizeToFit()
        bottonToAdd.center = mapViewForScreen.center
        bottonToAdd.tintColor = UIColor.blue
        mapViewForScreen.addSubview(bottonToAdd)
        mapViewForScreen.bringSubviewToFront(bottonToAdd)
    

提交回复
热议问题