首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何创建自定义MKAnnotationView和自定义批注标题和副标题

如何创建自定义MKAnnotationView和自定义批注标题和副标题
EN

Stack Overflow用户
提问于 2013-04-27 21:35:58
回答 1查看 39K关注 0票数 18

我需要在MKMapView上创建上面的注释视图。我可以创建自定义注释视图,但在点击注释时,视图需要打开带有大文本的图像,我无法创建该视图。请给我提供一些链接或完成这项任务的方法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-27 22:52:27

要创建自定义注释视图(标准管脚的替代品),只需在viewForAnnotation方法中设置MKAnnotationViewimage属性:

代码语言:javascript
复制
- (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。这给了你一个令人惊讶的控制程度,添加各种图像,标签等。

如果要彻底重新设计标注,可以让viewForAnnotationcanShowCallout设置为NO,然后在自定义注释视图中响应setSelected以显示您自己的标注。在Swift中,请参见Customize MKAnnotation Callout View?,了解一些用于自定义标注的选项。

票数 37
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16252764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档