【问题标题】:MKAnnotationView custom button imageMKAnnotationView 自定义按钮图片
【发布时间】:2014-06-15 00:10:04
【问题描述】:

我试图在我的MKAnnotationView 上使用自定义图像,当我使用以下代码时,我的注释上没有图像。我已经检查了调试以确保图像被正确加载到UIImage

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {


    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"String"];
    if(!annotationView) {

        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"String"];
        UIButton *directionButton = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *directionIcon = [UIImage imageNamed:@"IconDirections"];

        [directionButton setImage:directionIcon forState:UIControlStateNormal];

        annotationView.rightCalloutAccessoryView = directionButton;
    }

    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;

    return annotationView;
}

【问题讨论】:

    标签: ios objective-c uibutton mkannotationview


    【解决方案1】:

    主要有两个问题:

    1. 自定义标注按钮的frame 未设置,使其基本上不可见。
    2. 正在创建MKAnnotationView,但未设置其image 属性(注释本身的图像- 不是标注按钮的图像)。这会使整个注释不可见。

    对于问题 1,将按钮的框架设置为某个适当的值。例如:

    UIImage *directionIcon = [UIImage imageNamed:@"IconDirections"];
    directionButton.frame = 
        CGRectMake(0, 0, directionIcon.size.width, directionIcon.size.height);
    

    对于问题 2,设置注释视图的 image(或创建一个 MKPinAnnotationView):

    annotationView.image = [UIImage imageNamed:@"SomeIcon"];
    


    此外,您应该通过更新 annotation 属性来正确处理视图重用。
    完整示例:

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
    {    
        MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"String"];
        if(!annotationView) {
    
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"String"];
    
            annotationView.image = [UIImage imageNamed:@"SomeIcon"];
    
            UIButton *directionButton = [UIButton buttonWithType:UIButtonTypeCustom];
            UIImage *directionIcon = [UIImage imageNamed:@"IconDirections"];
            directionButton.frame = 
                CGRectMake(0, 0, directionIcon.size.width, directionIcon.size.height);
    
            [directionButton setImage:directionIcon forState:UIControlStateNormal];
    
            annotationView.rightCalloutAccessoryView = directionButton;
            annotationView.enabled = YES;
            annotationView.canShowCallout = YES;
        }
        else {
            //update annotation to current if re-using a view
            annotationView.annotation = annotation;
        }    
    
        return annotationView;
    }
    

    【讨论】:

      【解决方案2】:

      为了显示标注,必须选择注释。要以编程方式执行此操作,请调用:

      [mapView selectAnnotation:annotation animated:YES];
      

      其中annotation 是您希望为其显示标注的特定MKAnnotation

      你几乎肯定会想把它放在- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views

      有一些注意事项需要考虑,所以这里有另外两篇有一些很好的答案和相关讨论的帖子:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-23
        • 2016-02-10
        相关资源
        最近更新 更多