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

时间:2013-07-04 15:12:53

标签: 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;

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

例外:

  

*由于未捕获的异常'NSGenericException'而终止应用,原因:'&gt;无法显示带有clipsToBounds的标注'   * 第一次抛出调用堆栈:   (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调用抛出异常   (lldb)

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:1)

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

答案 2 :(得分:0)

在您的代码中,重写:

self.layer.masksToBounds = NO;