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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With