Reputation: 12005
When clicking MKAnnotationViews on a map they are temporarily brought to the front. I don't want this behaviour has I have a lot of MKAnnotationViews that are just icons on the map that don't have any other function.
I've tried setting the MKAnnotationView's userInteractionEnabled
to NO
but it does not work. The MKAnnotationView still comes to the front when touched. This is confusing as MKAnnotationView is just a UIView so I can't work out why userInteractionEnabled
is being ignored.
Upvotes: 1
Views: 3291
Reputation: 2308
SWIFT 4 example
We could reach our goal - Disable touches on MKAnnotationView with one really simple delegate method:
extension MyViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
for view in views {
view.isEnabled = false
}
}
}
If the value of this property is false, the annotation view ignores touch events and cannot be selected. Combining with extension- and in summary, we get the elegant way of extending our ViewController with MKMapViewDelegate protocol conformance and unselectable MKAnnotationView!
Upvotes: 1
Reputation:
As mentioned in a previous answer (and the documentation), to disable touches on an annotation view, you can set its enabled
property to NO
.
Setting canShowCallout
to NO
is a potential alternative. However, that will not prevent the didSelectAnnotationView
and didDeselectAnnotationView
delegate methods from still getting called (even though the callout will not be displayed). That may be an issue depending on your situation.
Upvotes: 5
Reputation: 4249
Set MKPinAnnotationView *pinView or MKAnnotationView *annotationView property of user interaction to NO in the method where u have created them i.e
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation{
static NSString *annotation = @"identifier";
MKAnnotationView * aView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( aView == nil )
{
aView = [[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:annotation] ;
}
[aView setUserInteractionEnabled:NO];
}
Upvotes: 0
Reputation: 1408
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinView = nil;
//NSLog(@"my loc : %@",mapView.userLocation);
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = @"any";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
{
pinView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID] ;
}
//this property may help you
pinView.canShowCallout = NO;
}
Upvotes: 2