使用MapView(MapQuest)上的按钮的子视图不可单击

时间:2014-07-09 09:17:46

标签: ios objective-c uiview mapquest

我在viewDidLoad方法中添加了一个视图

- (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];
}

在我的viewForAnnotation方法中,我定义了一个注释视图

-(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;
}

当我点击图标时,会调用didSelectAnnotationView

- (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];

}

子视图(UIView)可见(位于地图视图的顶部)但按钮不可点击,因为永远不会调用方法“button_pressed”。

我试图在子视图上将“userInteractionEnabled”设置为“YES”,但它不起作用。

1 个答案:

答案 0 :(得分:0)

我通过使用以下方法在Swift中解决了此问题:

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

因此在示例中:

 insertSubview([YOUR_SUBVIEW_TO_TAP_ON], 
          aboveSubview [MAPVIEW])

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