MKMapView ignores update of centerOffset in iOS 4

2019-02-12 07:44发布

I previously created a custom callout bubble as a subview to the MKAnnotationView because the built in callout is so limited. This requires me to change to centerOffset of the MKAnnotationView when it is selected to account for the size of the callout bubble. This all worked perfectly before iOS 4 came out. Now, with iOS 4, it completely ignores my updating of the centerOffset property and therefore the pin and bubble appear to jump down and to the right (the top left corner of the callout bubble is now at the location where the pin point should be).

Does anyone know why this has changed in iOS 4? Is there something I can do to get the MKMapView to recognize the new centerOffset? Is this a bug that apple introduced?

Thanks for the help!

3条回答
兄弟一词,经得起流年.
2楼-- · 2019-02-12 08:02

I have the same problem - centerOffset seems to be taken into account only the first time. It is changed internally, but the view is not moved - so what you need to do is move the view yourself.

You can move the view by adjusting its center with the required offset - the selected view remains aligned at the top-left corner with the unselected view, so you need to realign their centers. Here's my case:

Selected -> Unselected:

self.center = CGPointMake(self.center.x + 56.0, self.center.y + 130.0);
self.centerOffset = CGPointMake(5.0, -14.0);

Unselected -> Selected:

self.center = CGPointMake(self.center.x - 56.0, self.center.y - 130.0);
self.centerOffset = CGPointMake(64.0, -81.0);

Where 130 is the difference in height between the views(center point is at the bottom), and 56 is the difference between the X offsets of their centers.

Remember - you still need to change the center offset because it'll be taken into account when zooming.

Hope this helps, I've lost a few hours on this. Remember to submit a bug report to Apple.

查看更多
Ridiculous、
3楼-- · 2019-02-12 08:24

Make sure you are using MKAnnotationView and not MKPinAnnotationView! You can't set the centerOffset of a MKPinAnnotationView-object (except if you subclass of course).

查看更多
萌系小妹纸
4楼-- · 2019-02-12 08:24

I think instead of centerOffset you can use setRegion which works fine in all versions.

CGPoint point = [mapView convertCoordinate:selectedAnnotation.coordinate toPointToView:self.view];

CGRect frame = [customView frame];
frame.origin.y = point.y - frame.size.height;
frame.origin.x = point.x - frame.size.width / 2;

MKCoordinateRegion region = [mapView convertRect:frame toRegionFromView:self.view];
[mapView setRegion:region animated:YES];
查看更多
登录 后发表回答