如何显示 MKPinAnnotationView.rightCalloutAccessoryView 的箭头,就像在 Apple 地图中一样?
How do I display an arrow for MKPinAnnotationView.rightCalloutAccessoryView, like in Apple Maps?
我是只用类似的图像初始化一个按钮,还是有任何我可以使用的默认值?
MKPinAnnotationView *newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinLocation"];
newAnnotation.animatesDrop = YES;
newAnnotation.canShowCallout = YES;
newAnnotation.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
您可以使用以下代码显示披露按钮:
- (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;
}
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
}
}
我是只用类似的图像初始化一个按钮,还是有任何我可以使用的默认值?
MKPinAnnotationView *newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinLocation"];
newAnnotation.animatesDrop = YES;
newAnnotation.canShowCallout = YES;
newAnnotation.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
您可以使用以下代码显示披露按钮:
- (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;
}
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
}
}