Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get frame of annotation on map view

In my app I have custom annotations where the leftCalloutAccessoryView is a circular UIView. This works perfectly in the simulator with this code:

    let rideTimeView = UIView()
    rideTimeView.frame = CGRectMake(5, 5, 50, 50)
    rideTimeView.layer.cornerRadius = 25

The problem arises when I run the app on actual devices. It works fine on (I believe) 6 and 6 Plus, but overlaps at the bottom on 4 and 5.

Is there any way I can get the dimensions of the annotation?

like image 753
user3746428 Avatar asked Aug 17 '15 23:08

user3746428


Video Answer


1 Answers

Swift 3:

To get the frame of MKAnnotationView in MKMapView, first you will need a reference to the MKAnnotation. Then do:

if let annotationView = self.mapView.view(for: annotation) {
    let frameOfAnnotationViewInMapView = annotationView.frame
}

In addition, if you want the frame of the MKAnnotationView on screen, do:

let frameOfViewInSuperview = self.mapView.convert(frameOfAnnotationViewInMapView, to: self.mapView.superview?.superview?.superview?.superview?.superview?.superview?.superview?.superview?.superview?.superview?.superview?.superview?.superview?.superview?.superview)

The number of times you need to call .superview will depend on how deep your MKMapView is nested. For my case, it was nested very deep.

like image 165
Quy Bui Avatar answered Sep 19 '22 15:09

Quy Bui