Ios 如何使用swift 4在GMSMapView上添加按钮

Ios 如何使用swift 4在GMSMapView上添加按钮,ios,dictionary,button,swift4,google-maps-sdk-ios,Ios,Dictionary,Button,Swift4,Google Maps Sdk Ios,这是我的视图控制器,即使我向CGRect添加值,它也不起作用 import UIKit import GoogleMaps class ViewController: UIViewController,CLLocationManagerDelegate, GMSMapViewDelegate { override func viewDidLoad() { super.viewDidLoad() let camera = GMSCameraPosition.camera(withLati

这是我的视图控制器,即使我向CGRect添加值,它也不起作用

import UIKit
import GoogleMaps

class ViewController: UIViewController,CLLocationManagerDelegate, 
GMSMapViewDelegate {

 override func viewDidLoad() {
super.viewDidLoad()

let camera = GMSCameraPosition.camera(withLatitude: 52.649030, 
longitude: 1.174155, zoom: 14)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView.delegate = self
self.view = mapView

let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: 52.649030, 
longitude: 1.174155)
marker.title = "marker1"
marker.snippet = "city"
marker.map = mapView
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
self.view.addSubview(button)

}

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
 print(marker.position)
 return true
  }
  }
那么,我的代码中有错误吗?
谢谢

您的代码没有问题。按钮实际上在那里。 如果检查图层,可以在左上角看到,如下所示:

只需添加按钮标题/标题颜色。或者背景色,你会看到的。

移动按钮并设置标题后,它将如下所示:
您的代码没有问题。按钮实际上在那里。 如果检查图层,可以在左上角看到,如下所示:

只需添加按钮标题/标题颜色。或者背景色,你会看到的。

移动按钮并设置标题后,它将如下所示:

CGRect为空。试试:
ui按钮(frame:CGRect(x:0,y:0,width:30,height:30))
对不起,我是新来的这是我的第一个问题,但仍然没有显示按钮添加文本或按钮背景色来查看mapviewBrother上的按钮。您确实需要先查看在线教程,您似乎对iOS编程相当陌生。祝你好运CGRect为空。试试:
ui按钮(frame:CGRect(x:0,y:0,width:30,height:30))
对不起,我是新来的这是我的第一个问题,但仍然没有显示按钮添加文本或按钮背景色来查看mapviewBrother上的按钮。您确实需要先查看在线教程,您似乎对iOS编程相当陌生。祝你好运谢谢你的帮助,我现在可以看到按钮了。还有一个问题,当点击按钮时,如何做一些事情(我如何处理这个事件)?将目标添加到button button.addTarget(self,action:#selector(handleTap),for:.touchUpInside)并用它做一些事情:func handleTap(u发送者:UIButton){print(“你点击了一个按钮”)}这就是我要找的。非常感谢你的帮助我现在可以看到按钮了。还有一个问题,当点击按钮时,如何做一些事情(我如何处理这个事件)?将目标添加到button button.addTarget(self,action:#selector(handleTap),for:.touchUpInside)并用它做一些事情:func handleTap(u发送者:UIButton){print(“你点击了一个按钮”)}这就是我要找的。非常感谢你。
    let button = UIButton(frame: CGRect(x: 50, y: 50, width: 100, height: 100))

button.setTitle("Button", for: .normal)
button.setTitleColor(.red, for: .normal)
button.addTarget(self, action: #selector(handleTap), for: .touchUpInside)
self.view.addSubview(button)

@objc func handleTap(_ sender: UIButton) {
    print("You tapped a button")
}