Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom MKAnnotationView masksToBounds=YES and canShowCallout=YES exception

How to I properly set a radius on a custom MKAnnotationView and allow callouts? This throws an exception:

From my custom MKAnnotationView class:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self)
    {
        self.frame = CGRectMake(0, 0, 30, 30);
        self.opaque = NO;

        self.multipleTouchEnabled = NO;
        self.backgroundColor = [UIColor whiteColor];

        self.layer.cornerRadius = 5;
        self.layer.masksToBounds = YES;

        self.layer.borderColor = [UIColor blueColor].CGColor;
        self.layer.borderWidth = 1.0f;

        self.layer.shadowOffset = CGSizeMake(1, 0);
        self.layer.shadowColor = [[UIColor blackColor] CGColor];
        self.layer.shadowRadius = 5;
        self.layer.shadowOpacity = .25;
    }

    return self;
}

Within my class that presents the map view:

- (MKAnnotationView *)mapView:(MKMapView *)mView viewForAnnotation:(id <MKAnnotation>)annotation
{
    // logic to dequeue annotation views, etc.

    annotationView.canShowCallout = YES;        
    return annotationView;
}

Exceptions:

* Terminating app due to uncaught exception 'NSGenericException', reason: '> cannot show callout with clipsToBounds enabled' * First throw call stack: (0x1c04012 0x1689e7e 0x4eab5f 0x4ebcaf 0x4ebaea 0x4ebf03 0x4d7e24 0x4d7e54 0x4da610 0x5a7e 0x221853f 0x222a014 0x221a7d5 0x1baaaf5 0x1ba9f44 0x1ba9e1b 0x1a617e3 0x1a61668 0x5cdffc 0x29ad 0x28d5) libc++abi.dylib: terminate called throwing an exception (lldb)

like image 498
mservidio Avatar asked Dec 04 '25 06:12

mservidio


2 Answers

I found a solution. I created an OuterAnnotationView with a clear color and added a subview to the OuterAnnotationView on initialization. The subview that I added (the inner) I can set a cornerRadius and masksToBounds=YES and everything works now.

like image 112
mservidio Avatar answered Dec 05 '25 22:12

mservidio


-(MKAnnotationView *)annotationView{



    annotationView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:profilePictureString]]];
    annotationView.canShowCallout = NO;
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeInfoDark];

    annotationView.frame = CGRectMake(0, 0, 50,50);
    annotationView.layer.cornerRadius = 25;
    annotationView.layer.masksToBounds = YES;
    annotationView.contentMode = UIViewContentModeScaleAspectFit;


    return annotationView;
}
like image 23
ericdmann Avatar answered Dec 06 '25 00:12

ericdmann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!