我如何显示MKPinAnnotationView.rightCalloutAccessoryView的箭头,就像在Apple Maps中一样?

问题描述 投票:2回答:2

我是否只是使用类似图像初始化按钮,或者可以使用任何默认值?

enter image description here

MKPinAnnotationView *newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinLocation"];
newAnnotation.animatesDrop = YES;
newAnnotation.canShowCallout = YES;
newAnnotation.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
ios objective-c mkmapview
2个回答
1
投票

您可以使用以下代码显示公开按钮:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{   
    MKPinAnnotationView *newAnnotation = [[MKPinAnnotationView alloc]     initWithAnnotation:annotation reuseIdentifier:@"pinLocation"];

    newAnnotation.canShowCallout = YES;
    newAnnotation.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

 //for custom button 
UIImage *btnImage = [UIImage imageNamed:@"arrow.png"];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:btnImage forState:UIControlStateNormal];

  newAnnotation.rightCalloutAccessoryView = btn;

//try this for custom image on callout accessory view
   newAnnotation.rightCalloutAccessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.png"]];

    return newAnnotation;
}

0
投票

Swift 4.2:您可以按如下所示在Swift 4.2中显示披露按钮:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? 
{

    if annotation is MKUserLocation { return nil }

    if let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "") {
        annotationView.annotation = annotation
        return annotationView
    } else {
        let annotationView = MKPinAnnotationView(annotation:annotation, reuseIdentifier:"")
        annotationView.isEnabled = true
        annotationView.canShowCallout = true

        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "arrow"), for: .normal)
        btn.frame = CGRect.init(x: 0, y: 0, width: 20, height: 30)
        annotationView.rightCalloutAccessoryView = btn
        btn.addTarget(self, action:#selector(buttonClicked(_:)), for: .touchUpInside)

        return annotationView
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.