MKPinAnnotationView子视图不会更新

时间:2013-09-02 13:48:57

标签: ios objective-c mkmapview mkpinannotationview

我正在使用自定义AnnotationViews的应用程序。它有一个自定义图像和一个带有标签的背景。背景和标签是annotationView的子视图。

if ([annotation isKindOfClass:[VehicleAnnotation class]]) {
    VehicleAnnotation *vehicleAnnotation = annotation;

    MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:driverAnnotationIdentifier];
    if(!pinView)
    {
        pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:driverAnnotationIdentifier];
    }

    pinView.annotation = annotation;

    pinView.canShowCallout = NO;
    pinView.image = [ImageHelper imageForStatus:vehicleAnnotation.vehicle.driver.driverStatus];

    // Add a label with the name of the driver
    CGRect pinFrame = pinView.frame;
    UILabel *label = [[UILabel alloc] init];
    [label setBackgroundColor:[UIColor clearColor]];
    [label setTextColor:[UIColor whiteColor]];
    [label setNumberOfLines:2];
    [label setLineBreakMode:NSLineBreakByWordWrapping];
    [label setFont:[UIFont systemFontOfSize:12]];
    label.text = vehicleAnnotation.vehicle.driver.description;

    CGSize maximumSize = CGSizeMake(100, 50);
    CGSize myStringSize = [label.text sizeWithFont:label.font
                                 constrainedToSize:maximumSize
                                     lineBreakMode:label.lineBreakMode];

    CGRect labelFrame = CGRectMake(pinFrame.origin.x, pinFrame.origin.y + pinFrame.size.height * 2 + 2, myStringSize.width, myStringSize.height);
    [label setFrame:labelFrame];

    // Add a background to the label.
    CGFloat offset = 5;
    CGRect backgroundFrame = CGRectMake(labelFrame.origin.x - offset, labelFrame.origin.y - offset , labelFrame.size.width + 2 * offset, labelFrame.size.height + 2 * offset);
    UIView *backgroundView = [[UIView alloc] initWithFrame:backgroundFrame];
    backgroundView.layer.cornerRadius = 5;
    backgroundView.layer.masksToBounds = YES;
    [backgroundView setBackgroundColor:[UIColor darkGrayColor]];
    [backgroundView setAlpha:0.8];
    [pinView addSubview:backgroundView];

    [label setUserInteractionEnabled:YES];
    [label setMultipleTouchEnabled:YES];
    [pinView addSubview:label];

    return pinView;
}

每次收到新职位时都会执行此代码。问题是旧标签和旧背景仍然存在。所以我想我把这段代码:

MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:driverAnnotationIdentifier];
if(!pinView)
{
    pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:driverAnnotationIdentifier];
}
for(UIView *view in [pinView subviews])
{
    [view removeFromSuperview];
}
pinView.annotation = annotation;

但是没有显示标签或背景。有人可以帮助我以适当的方式替换或删除背景和标签吗?

谢谢!

2 个答案:

答案 0 :(得分:2)

我建议你自己定制MKAnnotatinView。否则使用上面的代码,尝试在bock中添加标签和背景视图,在其中为init分配MKAnnotationView。设置框架和文本以及其他属性,这些属性会针对块外的不同注释进行更改,如下所示。

if ([annotation isKindOfClass:[VehicleAnnotation class]]) {
    VehicleAnnotation *vehicleAnnotation = annotation;

    MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:driverAnnotationIdentifier];
    if(!pinView)
    {
        pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:driverAnnotationIdentifier];
    UILabel *label = [[UILabel alloc] init];
    [label setBackgroundColor:[UIColor clearColor]];
    [label setTextColor:[UIColor whiteColor]];
    [label setNumberOfLines:2];
    [label setLineBreakMode:NSLineBreakByWordWrapping];
    [label setFont:[UIFont systemFontOfSize:12]];
    [label setUserInteractionEnabled:YES];
    [label setMultipleTouchEnabled:YES];
    [label setTag:101];

    UIView *backgroundView = [[UIView alloc]init];
    [backgroundView setTag:102];
    backgroundView.layer.cornerRadius = 5;
    backgroundView.layer.masksToBounds = YES;
    [backgroundView setBackgroundColor:[UIColor darkGrayColor]];
    [backgroundView setAlpha:0.8];
    [pinView addSubView:backgroundView];
    [pinView addSubview:label];



    }

    pinView.annotation = annotation;

    pinView.canShowCallout = NO;
    pinView.image = [ImageHelper imageForStatus:vehicleAnnotation.vehicle.driver.driverStatus];

    // set frame of Label
    CGRect pinFrame = pinView.frame;
   UILabel *label = (UILabel *)[pinView viewWithTag:101];
    label.text = vehicleAnnotation.vehicle.driver.description;

    CGSize maximumSize = CGSizeMake(100, 50);
    CGSize myStringSize = [label.text sizeWithFont:label.font
                                 constrainedToSize:maximumSize
                                     lineBreakMode:label.lineBreakMode];

    CGRect labelFrame = CGRectMake(pinFrame.origin.x, pinFrame.origin.y + pinFrame.size.height * 2 + 2, myStringSize.width, myStringSize.height);
    [label setFrame:labelFrame];

    // set backgroundView frame.
    CGFloat offset = 5;
    CGRect backgroundFrame = CGRectMake(labelFrame.origin.x - offset, labelFrame.origin.y - offset , labelFrame.size.width + 2 * offset, labelFrame.size.height + 2 * offset);
    UIView *backgroundView = [pinView viewWithTag:102];
    [backgroundView setFrame:backgroundFrame];

    return pinView;
}

答案 1 :(得分:1)

设置MKPinAnnotationView的image无效,它将覆盖它。使用常规的MKAnnotationView。

同时在Stackocerflow上搜索,这个问题在你问到它之前六个小时就被问到并回答了。 MapView Custom Pin Image Issue