ios - 尝试更改 MKPointAnnotation 颜色但丢失标题

标签 ios objective-c mkpinannotationview

我正在尝试将我的图钉颜色更改为紫色,但当我这样做时我失去了标题。代码是:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationController.navigationBarHidden=YES;

    //init the location manager
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [self.locationManager requestWhenInUseAuthorization];
    self.mapView.showsUserLocation=YES;

    self.userGeoPoint=self.message[@"userLocation"];
    self.pinView = [[MKPointAnnotation alloc] init];

    self.pinView.title=self.message[@"fromUser"];
    self.coord = CLLocationCoordinate2DMake(self.userGeoPoint.latitude, self.userGeoPoint.longitude);
    self.pinView.coordinate=self.coord;
    //use a slight delay for more dramtic zooming
    [self performSelector:@selector(addPin) withObject:nil afterDelay:0.5];
}

-(void)addPin{

    [self.mapView addAnnotation:self.pinView];
    [self.mapView selectAnnotation:self.pinView animated:YES];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.coord, 800, 800);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];

}

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotationPoint
{
    if ([annotationPoint isKindOfClass:[MKUserLocation class]])//keep the user as default
        return nil;

    static NSString *annotationIdentifier = @"annotationIdentifier";
    MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotationPoint reuseIdentifier:annotationIdentifier];
    pinView.pinColor = MKPinAnnotationColorPurple;
    //now we can throw an image in there

    return pinView;
}

我尝试为 MKPinAnnotation 设置标题属性,但没有一个。无论如何我可以解决这个问题吗?

最佳答案

viewForAnnotation中,需要将canShowCallout设置为YES(默认为NO):

pinView.pinColor = MKPinAnnotationColorPurple;
pinView.canShowCallout = YES;



几个不相关的点:

  1. 看起来您有一个名为 pinView 的属性,类型为 MKPointAnnotation,您正在使用它在 viewDidLoad 中创建注释对象。然后在 viewForAnnotation 中,您有一个名为 pinViewMKPinAnnotationView 类型的局部变量。这里虽然没有命名上的冲突,但是却造成并暗示了一些概念上的困惑。 MKPointAnnotation 是一个注释 model 类,而 MKPinAnnotationView 是一个注释 view 类——它们是完全不同的东西。例如,将注释属性命名为 pinuserAnnotation 会更好。
  2. viewForAnnotation 中的注释:

    //now we can throw an image in there
    

    似乎暗示此时您可以在 View 中设置自定义图像。不建议在 MKPinAnnotationView 中设置自定义图像,因为该类旨在以三种颜色之一显示默认图钉图像。要使用自定义图像,请改为创建普通的 MKAnnotationView

关于ios - 尝试更改 MKPointAnnotation 颜色但丢失标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29315590/

相关文章:

ios - Xcode 6 中的大小类问题

ios - 无法映射单个对象

iphone - 如何在 iPhone 中以编程方式检查设备的方向?

ios - 防止转义 URL 中的括号

ios - MKPinAnnotationView 不起作用

ios - MKPinAnnotationView 不显示标题

ios - 不同的结束编辑原因不同的 Action

iphone - 方向问题 - ipad

ios - MKPinAnnotationView:引脚宽度和高度

ios - 是什么导致我的 "unrecognized selector"异常?