9

我需要一些可以在 mk* 函数之外使用的代码。我需要运行我的自定义函数以将数组中的 FIRST 和 LAST 标记放在前面。(所以在我屏幕上的所有其他标记之上)。我努力了

[self.view bringSubviewToFront:[[mapView annotations]objectAtIndex: 0]];

我已经[[mapView annotations]objectAtIndex: 0]在我的代码中使用了它并且有效,但是当我尝试将它放在前面时它会崩溃。我是在访问错误的图层还是什么?

谢谢你的帮助。

4

3 回答 3

13

你把错误的东西带到了前面。即,annotations数组是符合MKAnnotation协议的对象数组(类型为so id<MKAnnotation>),它们不是视图。

相反,您应该获得所需注释的视图,并将它们放在前面:

id<MKAnnotation> annotation = [[mapView annotations] objectAtIndex:0];
MKAnnotationView* annotationView = [mapView viewForAnnotation:annotation];
if (annotationView != nil) {
    [annotationView.superview bringSubviewToFront:annotationView];
}

但是,您应该注意以下几点:

  1. annotationView可能是nil注释位于屏幕外的某个点上,或者注释尚未完成添加到地图中。但是,如果它是nil,您可能并不关心甚至不在屏幕上的注释是否在前面。
  2. 您需要调用bringSubviewToFrontannotationViewsuperview,而不是 onself.view甚至mapView,因为它们都不是annotationView.
于 2010-09-15T08:26:18.237 回答
1

这对我来说效果更好:

在以下位置设置注释视图层的 zPosition (annotationView.layer.zPosition):

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    // if it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    MKAnnotationView *returnedAnnotationView = nil;
    returnedAnnotationView = [CUSTOMVIEW createViewAnnotationForMapView:self.mapView annotation:annotation];
    // SOME CUSTOM PROCESSING...
    returnedAnnotationView.layer.zPosition = 3;
    return returnedAnnotationView;
}

请注意,我假设默认 zPosition 为 0。设置为 3 会使所有 3 个标记都显示在我的顶部。

于 2016-03-30T00:14:48.367 回答
0
For IOS 11.0 onwards use below solution to bring the Custom Callout above all annotations.

Add z position observer where you are initialising the Custom callout 

[self.layer addObserver:self forKeyPath:@"zPosition" options:0 context:nil];


-(void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context
{
    if(object == self.layer)
    {
        self.layer.zPosition = FLT_MAX;
    }
}
于 2019-10-04T08:44:53.013 回答