Objective c 自定义MKAnnotationView masksToBounds=YES和canShowCallout=YES异常

Objective c 自定义MKAnnotationView masksToBounds=YES和canShowCallout=YES异常,objective-c,ios6,uiview,mkmapview,mkannotationview,Objective C,Ios6,Uiview,Mkmapview,Mkannotationview,如何在自定义MKAnnotationView上正确设置半径并允许标注?这会引发一个异常: 从我的自定义MKAnnotationView类: - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.frame = CGRectMake(0, 0, 30, 30); self.opaque = NO; s

如何在自定义MKAnnotationView上正确设置半径并允许标注?这会引发一个异常:

从我的自定义MKAnnotationView类:

- (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;
}
在显示地图视图的我的类中:

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

    annotationView.canShowCallout = YES;        
    return annotationView;
}
-(MKAnnotationView*)映射视图:(MKMapView*)mView视图用于注释:(id)注释
{
//将注释视图等出列的逻辑。
annotationView.canShowCallout=是;
返回注释视图;
}
例外情况:

*由于未捕获的异常“NSGenericeException”而终止应用程序,原因:“>无法在启用clipsToBounds的情况下显示调用” *第一次抛出调用堆栈: (0x1c04012 0x1689e7e 0x4eab5f 0x4ebcaf 0x4EBEAA 0x4ebf03 0x4d7e24 0x4d7e54 0x4da610 0x5a7e 0x221853f 0x222a014 0x221a7d5 0x1BAAF5 0x1ba9f44 0x1ba9e1b 0x1a617e3 0x1a61668 0x5cdffc 0x29ad 0x28d5) libc++abi.dylib:terminate调用引发异常 (lldb)


我找到了解决办法。我创建了一个带有清晰颜色的OuterAnnotationView,并在初始化时向OuterAnnotationView添加了一个子视图。我添加的子视图(内部)可以设置
a cornerRadius and masksToBounds=是,现在一切正常。

在您的代码中,使用以下代码重新编写:

-(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;
}

self.layer.masksToBounds=否

您是否必须对外部注释视图的框架大小进行任何处理,以使其与子视图的大小正确匹配?