MapKitのMKMapViewを使って地図を表示するアプリを開発しています。
MKAnnotationViewを継承した CustomAnnotationView をタップしても MKMapViewDelegate#mapView(_:, didSelect:)
が呼ばれず困っていました。
タップしてもdidSelectが呼ばれない
ハマった。
Stack over flowを参考に、CustomAnnotationViewにサイズを与えた。
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { if annotation === mapView.userLocation { return nil } let identifier = "annotation" var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) if pinView == nil { pinView = CustomAnnotationView(annotation: annotation, reuseIdentifier: identifier) } pinView?.frame = CGRect(x: 0, y: 0, width: 49, height: 60) // ← これが必要だった pinView?.annotation = annotation pinView?.layoutIfNeeded() return pinView }
関連記事
このほかにもiOSアプリ開発で見つけたネタや悩んだ内容など紹介しています。Tipsをまとめておりますのでこちらのページをご参照ください。