只能通过子类化 MKMapView 来自定义 MKAnnotationView?
我已经尝试过这个(MKMapView Delegate):
-(void) mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
for (MKAnnotationView *annotationView in views)
{
annotationView.image = [UIImage imageNamed:@"itemType2.png"];
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.canShowCallout = YES;
}
}
和这个(在具有 MKMapView 的 VC 上):
[self.theMapView addAnnotations:annotationsArray];
for (id <MKAnnotation> itemAnnotation in self.theMapView.annotations)
{
MKAnnotationView *itemAnnotationView = [self.theMapView viewForAnnotation:itemAnnotation];
itemAnnotationView.image = [UIImage imageNamed:@"itemType2.png"];
itemAnnotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
itemAnnotationView.canShowCallout = YES;
//[self.theMapView setNeedsDisplay];
}
在更改 MKAnnotationView 的外观方面没有任何成功,它们显示为简单的红色引脚,没有公开按钮或任何东西......
是通过创建子类 MKMapView 并使用 - (MKAnnotationView *)viewForAnnotation:(id < MKAnnotation >)annotation 来更改它们的唯一方法???我觉得没有必要仅仅为了更改注释而创建一个额外的子类,为什么上面的方法不起作用?
I've tried both this (MKMapView Delegate):
-(void) mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
for (MKAnnotationView *annotationView in views)
{
annotationView.image = [UIImage imageNamed:@"itemType2.png"];
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.canShowCallout = YES;
}
}
and this (on the VC that has the MKMapView):
[self.theMapView addAnnotations:annotationsArray];
for (id <MKAnnotation> itemAnnotation in self.theMapView.annotations)
{
MKAnnotationView *itemAnnotationView = [self.theMapView viewForAnnotation:itemAnnotation];
itemAnnotationView.image = [UIImage imageNamed:@"itemType2.png"];
itemAnnotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
itemAnnotationView.canShowCallout = YES;
//[self.theMapView setNeedsDisplay];
}
whithout any success on changing the MKAnnotationView's appearance, they appear as simple red pins without the disclosure buttons or anything....
Is the only way of changing them through creating a subclassed MKMapView and using - (MKAnnotationView *)viewForAnnotation:(id < MKAnnotation >)annotation ??? I feel it's desnecessary to create an extra subclass just for changing the annotations, why don't the above methods work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要对任何内容进行子类化即可在
MKAnnotationView
上设置这些属性。您需要实现
viewForAnnotation
delegate 方法并在其中设置属性。有关代码示例,请参阅此答案。
该示例的唯一变化是使用
MKAnnotationView
而不是MKPinAnnotationView
。You don't need to subclass anything to set those properties on an
MKAnnotationView
.You need to implement the
viewForAnnotation
delegate method and set the properties there.See this answer for a code example.
The only change from that example is to use
MKAnnotationView
instead ofMKPinAnnotationView
.