Pergunta

I have a view with a little mapView inside. I have a custom annotationView to mark the user position. Everything works ok as usual but I've realize that the annotation is not clipped to the map view so, when I drag the map, it goes out of bounds. Check the pictures:

Befrore dragging

When dragging

The mapView has the Clip Subviews enabled. I think this is the first time I found this behavior. Maybe I've never placed a map inside a bigger view with space around. I'm targeting iOS7 by the way.

EDIT: Here's the code. Nothing out of ordinary, I think.

Here, I add the custom annotation:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
static NSString *const kAnnotationIdentifier = @"DYNAnnotationView";
DYNAnnotationView *annotationView = (DYNAnnotationView *)
[mapView dequeueReusableAnnotationViewWithIdentifier:kAnnotationIdentifier];
if (! annotationView)
{
    annotationView = [[DYNAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:kAnnotationIdentifier];
}

[annotationView setAnnotation:annotation];

return annotationView;
}

And here is the custom annotationView:

DYNAnnotation.h

#import <MapKit/MapKit.h>

@interface DYNAnnotationView : MKAnnotationView

- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier;


@end

DYNAnnotation.m

#import "DYNAnnotationView.h"

@implementation DYNAnnotationView


-(id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self)
{
    UIImage *image = [UIImage imageNamed:@"locationMark"];
    CGRect frame = [self frame];
    frame.size = [image size];
    [self setFrame:frame];
    [self setCenterOffset:CGPointMake(0.5f, 1.0f)];
    [self setImage:image];
}

return self;
}
Foi útil?

Solução

OK. The solution was easy after all. All I needed to do was embed the mapView inside another view and set its clip subview property to YES. Still don't understand why the map view doesn't take care of this.

Hope this help somebody.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top