问题描述
我有一个地图视图,或多或少添加这样的注释:
I have a map view that adds annotations more or less like this:
- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id <MKAnnotation>) annotation
{
MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"MKPinAnnotationView"];
annotationView.canShowCallout = YES;
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[detailButton addTarget:self
action:@selector(handleButtonAction)
forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = detailButton;
return annotationView;
}
在iOS 7中,这会在右侧显示i图标标注的一面。点击图标触发 mapView:annotationView:calloutAccessoryControlTapped:
(在委托上)和 handleButtonAction:
(自我)。不过,我最近才意识到你也可以点击标注上的其他任何地方,并且会触发相同的两种方法。
In iOS 7, this puts an "i" icon on the right-hand side of the callout. Tapping on the icon triggers mapView:annotationView:calloutAccessoryControlTapped:
(on the delegate) and handleButtonAction:
(on self). I recently realized, though, that you can also tap anywhere else on the callout and the same two methods are fired.
这种情况发生在<$ c $类型的按钮上c> UIButtonTypeDetailDisclosure 但似乎没有 UIButtonTypeCustom
按钮。当没有附件视图时点击标注时,也不会触发委托方法。 (当然,这种行为并不令人惊讶;令人惊讶的是,如果附件视图是详细信息泄露按钮,则无论您是点击按钮本身还是仅仅点击某处,这两种方法都会被解雇其他在标注中。)
This happens with a button of type UIButtonTypeDetailDisclosure
but it doesn’t seem to happen with a UIButtonTypeCustom
button. The delegate method is also not fired when I tap on the callout when there’s no accessory view at all. (That behavior isn’t surprising, of course; what’s surprising is that if the accessory view is a detail-disclosure button then these two methods are fired regardless of whether you tap on the button itself or just somewhere else in the callout.)
我想摆脱标注中的按钮 - 或者至少用显示我自己的图像的按钮替换它而不是库存我图标 - 同时仍然允许用户点击标注上的任何位置来触发我的操作。这可能吗?我没有看到对应于callout tapped的 MKMapViewDelegate
方法。
I’d like to get rid of the button in the callout—or at least replace it with a button showing my own image instead of the stock "i" icon—while still allowing the user to tap anywhere on the callout to trigger my action. Is this possible? I don’t see an MKMapViewDelegate
method that corresponds to "callout tapped".
推荐答案
尝试为按钮设置自定义图像而不更改 UIButtonTypeDetailDisclosure
类型。
Try to set custom image for button without changing UIButtonTypeDetailDisclosure
type.
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[detailButton setImage:[UIImage imageNamed:@"icon"] forState:UIControlStateNormal];
对于iOS7及以上版本,此图像默认会着色。如果你想保留原始图标,请使用以下
For iOS7 and above this image will be tinted by default. If you want to keep original icon use the following
[[UIImage imageNamed:@"icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
或者如果你想删除图标
[detailButton setImage:[UIImage new] forState:UIControlStateNormal];
这篇关于允许在没有标注附件视图的情况下点击注释标注上的任何位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!