如何将自定义按钮/ MapPing添加到MKAnnotation?

时间:2013-02-28 13:16:20

标签: iphone uibutton mapkit customization mkannotation

我是iphone开发的菜鸟,我正在尝试为注释标注添加自定义按钮。我没有问题为rightCalloutAccessoryView添加常规按钮,但它只是不适用于自定义样式。我的图片大小为32 x 32.我还想在此stackoverflow问题here之后添加自定义地图图钉。非常感谢任何帮助。

我的代码

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
// Define your reuse identifier.
static NSString *identifier = @"MapPoint";   

if ([annotation isKindOfClass:[MapPoint class]]) {
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (annotationView == nil) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    } else {
        annotationView.annotation = annotation;
    }
    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;
    annotationView.animatesDrop = YES;
    annotationView.image = [UIImage imageNamed:@"myimage.png"];

    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton setImage:[UIImage imageNamed:@"phony2.png"] forState:UIControlStateNormal];
    [rightButton addTarget:self
                    action:@selector(showDetails:)
          forControlEvents:UIControlEventTouchUpInside];
    annotationView.rightCalloutAccessoryView = rightButton;

    return annotationView;
}
return nil;    

}

1 个答案:

答案 0 :(得分:2)

 [rightButton addTarget:self
                action:@selector(showDetails:)

删除此行 并设置按钮的框架

[rightButton setFrame:CGRectMake(0,0,32,32)];

并从degate方法获取点击操作

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;

您获取注释视图。

希望这会对你有所帮助。