简体   繁体   中英

Subview with button on a MapView (MapQuest) is not clickable

I add a view in my viewDidLoad method

- (void)viewDidLoad
{
   CGRect mapFrame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

   //Setup the map view
   mapView = [[MQMapView alloc] initWithFrame:mapFrame];
   mapView.mapZoomLevel = 15;
   mapView.delegate = self;

   [self.view addSubview:mapView];
   [self.view sendSubviewToBack:mapView];
}

In my viewForAnnotation method I define an annotationview

-(MQAnnotationView*)mapView:(MQMapView *)aMapView viewForAnnotation:(id<MQAnnotation>)annotation {

    static NSString* identifier = @"Pins";
    MQAnnotationView * annotationView = (MQAnnotationView *)[self->mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    annotationView = [[MQAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    annotationView.image = [UIImage imageNamed:@"marker_icon"];
    annotationView.userInteractionEnabled = YES;
    annotationView.enabled = YES;
    annotationView.canShowCallout = NO;
return annotationView;
}

When I tap on the icon the didSelectAnnotationView is called

- (void)mapView:(MQMapView *)mapView didSelectAnnotationView:(MQAnnotationView *)view {

    callOutView *calloutView = (callOutView *)[[[NSBundle mainBundle] loadNibNamed:@"callOutView" owner:self options:nil] objectAtIndex:0];
    CGRect calloutViewFrame = calloutView.frame;
    calloutViewFrame.origin = CGPointMake(-calloutViewFrame.size.width/2 + 15, -calloutViewFrame.size.height);
    calloutView.frame = calloutViewFrame;

    //Button Action
    [calloutView.button_in_xib addTarget:self action:@selector(button_pressed:) forControlEvents:UIControlEventTouchUpInside]; //the button is defined in storyboard (xib-file)

    calloutView.userInteractionEnabled = YES;

    [view addSubview:calloutView];

}

The subview (UIView) is visible (on the top of the mapview) but the button is not clickable because the method "button_pressed" is never called.

I tried to set "userInteractionEnabled" to "YES" on the subview but it didn't work.

I solved this in Swift by using:

func insertSubview(_ view: UIView, 
      aboveSubview siblingSubview: UIView)

So in example:

 insertSubview([YOUR_SUBVIEW_TO_TAP_ON], 
          aboveSubview [MAPVIEW])

https://developer.apple.com/documentation/uikit/uiview/1622570-insertsubview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM