ios 如何创建自定义 MKAnnotationView 和自定义注释标题和副标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16252764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:22:10  来源:igfitidea点击:

How to create Custom MKAnnotationView and custom annotation title and subtitle

iosmapmkannotationiphone-5mkannotationview

提问by Abhishek

enter image description here

在此处输入图片说明

I need to create above Annotation view on MKMapView. I am able to create the custom annotation view but on the tap of annotation the view need to be opened image with that big text, I am not able to create that one. Please provide me some links or the way to do this task.

我需要在 MKMapView 上创建上面的注释视图。我能够创建自定义注释视图,但是在单击注释时,视图需要使用大文本打开图像,我无法创建那个视图。请为我提供一些链接或完成此任务的方法。

回答by Rob

To create a custom annotation view (your replacement for the standard pin), you can just set the imageproperty of the MKAnnotationViewin the viewForAnnotationmethod:

要创建自定义注释视图(您替换标准引脚),您只需在方法中设置 的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;
}

You might also want to adjust the centerOffsetproperty to get the pin to line up precisely the way you want.

您可能还想调整该centerOffset属性以使图钉按照您想要的方式精确排列。

Regarding the customization of the callout, the easiest approach is to specify leftCalloutAccessoryView, rightCalloutAccessoryViewand/or detailCalloutAccessoryView. This gives you a surprising degree of control, adding all sorts of images, labels, etc.

关于标注的自定义,最简单的方法是指定leftCalloutAccessoryView,rightCalloutAccessoryView和/或detailCalloutAccessoryView。这为您提供了惊人的控制力,可以添加各种图像、标签等。

If you want to do a radical redesign of the callout, you can have viewForAnnotationset canShowCalloutto NOand then respond to setSelectedin your custom annotation view to show your own callout. While in Swift, see Customize MKAnnotation Callout View?for a few options for customizing the callouts.

如果您想对标注进行彻底的重新设计,您可以在自定义注释视图中viewForAnnotation设置canShowCalloutNO然后响应setSelected以显示您自己的标注。在 Swift 中,请参阅自定义 MKAnnotation 标注视图?用于自定义标注的几个选项。