【问题标题】:Subview with button on a MapView (MapQuest) is not clickableMapView(MapQuest)上带有按钮的子视图不可点击
【发布时间】:2014-08-30 05:56:24
【问题描述】:

我在 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”,但没有成功。

【问题讨论】:

  • 尝试将该按钮放在 UIView 中。
  • 谢谢拉詹,我已经试过了。我还尝试将按钮作为子视图添加到 calloutView-subview。当我点击按钮或子视图时,在 mapView 上调用点击事件并且地图正在放大。
  • 可以添加callOutView xib的图片吗?

标签: ios objective-c uiview mapquest


【解决方案1】:

我用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    相关资源
    最近更新 更多