Adding subview to MKMapView that's above the map but below the annotation views?

前端 未结 3 1165
感动是毒
感动是毒 2021-02-07 06:16

For an app I\'m building, I have a custom \"old map\" effect PNG that I\'d like to overlay on the MKMapView view. Unfortunately, neither way I\'ve tried to do this is exactly ri

3条回答
  •  太阳男子
    2021-02-07 06:30

    Note that all MKMapView subviews hierarchy, annotations, behaviour etc proceeds in internal private MKMapViewInternal class so changing anything (in a way I suggest or another) in this structure may cause your application to be rejected from Appstore.

    I do not have a full solution, just an idea. My idea is to make an overlay view have the same superview as annotation views and ensure that annotations will be placed over our overlay. In MKMapViewDelegate we implement:

    - (void)mapView:(MKMapView *)aMapView didAddAnnotationViews:(NSArray *)views{
        if (views.count > 0){
            UIView* tView = [views objectAtIndex:0];
    
            UIView* parView = [tView superview];
            UIView* overlay = [tView viewWithTag:2000];
            if (overlay == nil){
                overlay = [[UIImageView alloc] initWithImage:[UIImage imageNamed:MY_IMAGE_NAME]];
                overlay.frame = parView.frame; //frame equals to (0,0,16384,16384)
                overlay.tag = 2000;
                overlay.alpha = 0.7;
                [parView addSubview:overlay];
                [overlay release];
            }
    
            for (UIView* view in views)
                [parView bringSubviewToFront:view];
        }
    }
    

    This code does a half of what you want - you get a view placed between map tiles and annotations. The remaining task is to change custom overlay view frame according to map scrolling/zooming (I'll post if I find the way).

提交回复
热议问题