-->

如何创建自定义MKAnnotationView和自定义注解标题和副标题(How to create

2019-09-01 06:51发布

我需要创建上方的MKMapView注释视图。 我可以创建自定义的注解视图但标注的抽头认为需要打开图像与大的文本,我不能够创建一个。 请给我提供一些链接或做这个任务的方式。

Answer 1:

要创建自定义的注解视图(您更换为标准的引脚),你可以设置image的财产MKAnnotationViewviewForAnnotation方法:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
    {
        return nil;
    }
    else if ([annotation isKindOfClass:[YourAnnotationClassHere class]]) // use whatever annotation class you used when creating the annotation
    {
        static NSString * const identifier = @"MyCustomAnnotation";

        MKAnnotationView* annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

        if (annotationView)
        {
            annotationView.annotation = annotation;
        }
        else
        {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                          reuseIdentifier:identifier];
        }

        annotationView.canShowCallout = NO; // set to YES if using customized rendition of standard callout; set to NO if creating your own callout from scratch
        annotationView.image = [UIImage imageNamed:@"your-image-here.png"];

        return annotationView;
    }
    return nil;
}

您可能还需要调整centerOffset属性来获取销排队正是你想要的方式。

关于标注的定制,最简单的方法是指定leftCalloutAccessoryViewrightCalloutAccessoryView和/或detailCalloutAccessoryView 。 这给你控制的令人吃惊的程度,将各种图片,标签等。

如果你想要做标注的彻底的重新设计,你可以有viewForAnnotation设置canShowCalloutNO ,然后作出回应setSelected在您的自定义注解视图,以显示自己的标注。 虽然斯威夫特,看到自定义MKAnnotation标注查看? 对于定制标注几个选项。



文章来源: How to create Custom MKAnnotationView and custom annotation title and subtitle