How To add custom View in map's Annotation's Callout's

后端 未结 6 1226
野的像风
野的像风 2020-11-28 02:38

Here is my code. I want to add my own custom callout view instead of iOS default. I know there is only left callout and right callout view, but I need to add a view tooltip

6条回答
  •  盖世英雄少女心
    2020-11-28 03:13

    To the best answer above in Swift

    This can save some minutes to rewrite itself.

    !NOTE:

    There is one caveat which however can be easily fixed, your custom view will ignore touch events, because of mapView working with touches differently. Here's a quick fix:

    1) Subclass MKAnnotationView (or MKPinAnnotationView depends on what you need)

    2) in your mapView delegate

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
    

    use your subclass instead of MKAnnotationView/MKPinAnnotationView

    3) in your subclass .m file override two methods as following:

    override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
    
        let hitView = super.hitTest(point, withEvent: event)
    
        if (hitView != nil)
        {
            self.superview?.bringSubviewToFront(self)
        }
    
        return hitView;
    
    }
    
    override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
    
        let rect = self.bounds
        var isInside = CGRectContainsPoint(rect, point)
    
        if(!isInside) {
            for view in self.subviews {
    
                isInside = CGRectContainsPoint(view.frame, point)
                break;
            }
        }
    
        return isInside
    }
    

提交回复
热议问题