3

如何在自定义 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;
}

例外:

* 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:终止调用抛出异常 (lldb)

4

3 回答 3

2

我找到了解决方案。我创建了一个颜色清晰的 OuterAnnotationView,并在初始化时向 OuterAnnotationView 添加了一个子视图。我添加的子视图(内部)我可以设置cornerRadius 和maskToBounds=YES,现在一切正常。

于 2013-07-04T23:57:25.220 回答
2
-(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;
}
于 2014-10-04T19:27:22.877 回答
0

在您的代码中,重新编写它:

self.layer.masksToBounds = 否;

于 2015-10-07T14:06:31.553 回答