9

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?

user3746428
  • 11,047
  • 20
  • 81
  • 137
  • Refer this: http://stackoverflow.com/questions/11421732/convert-mkannotation-coordinates-to-view-coordinates Hope it helps – Mrunal Aug 26 '15 at 14:16

2 Answers2

2

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.

Quy Bui
  • 679
  • 5
  • 5
0

You can convert frame from superview to window:

let frame = convertRect(rideTimeView.frame, toView: UIApplication.sharedApplication().delegate!.window!)
gontovnik
  • 690
  • 5
  • 9
  • I've tried doing this - `let frame = pinView.convertRect(pinView.frame, toView: UIApplication.sharedApplication().delegate!.window!)`. pinView is the annotation that I need the frame for, but it just returns (0,0,0,0). – user3746428 Aug 20 '15 at 22:40
  • Try to use `pinView.superview.convertRect`. Let me know if it helps. – gontovnik Aug 21 '15 at 10:18
  • Didn't work unfortunately. I should mention that the above code is within `viewForAnnotation`, so at that point would pinView actually have a frame? – user3746428 Aug 21 '15 at 20:21
  • What does debugger show you when you write **"po pinView"** and **"po pinView.superview"**? – gontovnik Aug 21 '15 at 21:49
  • I get (0,0,0,0) with `pinView`, and the app just crashes with nil when I try `pinView.superview!`. – user3746428 Aug 21 '15 at 22:56
  • Did you try to debug view using button on debug panel? Or try to debug view by showing colored frames of each view element? – gontovnik Aug 21 '15 at 22:57