AnnotationView在calloutVIew上重叠

时间:2017-12-20 18:16:09

标签: ios objective-c mapkit mkannotationview

我正在使用MapKit在地图上显示数据。为此,我使用Custom AnnotationView和Custom CalloutView。但问题是当我让AnnotationView在那时相互关闭时,AnnotationView在CalloutView上重叠。以下是问题的截图。

overlapping annotationView

按钮也有问题,按钮的点击事件没有被调用。 calloutView下方的4个按钮不会被点击调用。但右上角的代表编辑事件的按钮在点击时会被触发。

这是我的CalloutView代码。

@implementation CustomCalloutView

- (id)init {

        return self;
}
- (IBAction)btnEditAction:(UIButton *)sender {

    [self.delegate btnEditClicked];
}

- (IBAction)btnMailAction:(UIButton *)sender {

    [self.delegate btnMailClicked];
}

- (IBAction)btnMessageAction:(UIButton *)sender {

    [self.delegate btnMessageClicked];
}

- (IBAction)btnCallAction:(UIButton *)sender {

    [self.delegate btnCallClicked];

}

- (IBAction)btnStreetAction:(UIButton *)sender {

    [self.delegate btnStreetClicked];
}
- (IBAction)CalloutTapGestureClicked:(UITapGestureRecognizer*)sender {
    [self.delegate CalloutTApGesture:sender];
}
@end

请帮我解决这个问题...

1 个答案:

答案 0 :(得分:0)

执行此操作以防止重叠(在顶部插入callOut视图)

 func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)
 {

   print("clickedddddddd")

    if view.annotation is MKUserLocation
    {
        return
    }

        let customView = (Bundle.main.loadNibNamed("customSou", owner: self, options: nil))?[0] as! customSouOut;

        var calloutViewFrame = customView.frame;
        calloutViewFrame.origin = CGPoint(x:-calloutViewFrame.size.width/2 + 15,y: -calloutViewFrame.size.height);
        customView.frame = calloutViewFrame;


        view.addSubview(customView)


         // here you can fill any label or button with data according to it's pin 

         //

        for v in view.subviews
        {
            if v is customSouOut
            {
                continue
            }
            else
            {
                view.insertSubview(customView, aboveSubview: v)
            }
        }

    }

}

func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView)
{
     print("oppspspspsps")

    if view.annotation is MKUserLocation
    {
        return
    }

     for v in view.subviews
    {
        if v is customSouOut 
        {
            v.removeFromSuperview()

            break
        }
    }




}