MKMapView在iOS8上选择注释时显示空白

3

我在iOS 8中遇到了MKMapview的一些奇怪行为。 我有一个自定义视图(UIView的子类),其中包含MKMapview。

NM视图 -> MKMapView

NM视图代码

#import "NMView.h"

@implementation NMView

@synthesize mapView;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        frame=self.bounds;
        self.translatesAutoresizingMaskIntoConstraints=NO;
        mapView=[[MKMapView alloc]initWithFrame:frame];
        mapView.showsUserLocation=YES;
        [mapView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
        [self setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
        mapView.delegate=self;
        [self addSubview:mapView];
        self.backgroundColor=[UIColor grayColor];
    }
    return self;
}
-(void)addCustomAnnotaion:(id<MKAnnotation>)annot{
  MKCoordinateRegion region=MKCoordinateRegionMake(annot.coordinate, MKCoordinateSpanMake(10.0f, 10.0f));
   [mapView setRegion:region];
    [mapView addAnnotation:annot];
   // [mapView selectAnnotation:annot animated:YES];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapViews viewForAnnotation:(id <MKAnnotation>)annotation
{

    if ([annotation isKindOfClass:[MKUserLocation class]]){
        return nil;
    }
    static NSString * const identifier = @"MyCustomAnnotation";
    MKAnnotationView* annotationView = (MKAnnotationView*)[mapViews dequeueReusableAnnotationViewWithIdentifier:identifier];

    if (annotationView){
        annotationView.annotation = annotation;
    }
    else{
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        annotationView.canShowCallout = YES;
        UIView *rightView=[[UIView alloc]initWithFrame:CGRectMake(5, 0, 80,50)];
        rightView.backgroundColor=[UIColor clearColor];

        UIButton* edit = [UIButton buttonWithType:UIButtonTypeCustom];
        [edit setImage:[UIImage imageNamed:@"editPOI"] forState:UIControlStateNormal];
        [edit addTarget:self action:@selector(editPoi:) forControlEvents:UIControlEventTouchUpInside];
        edit.backgroundColor=[UIColor clearColor];
        edit.frame=CGRectMake(15, 5, 30,30);

        UIButton* delButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [delButton setImage:[UIImage imageNamed:@"deletePOI"] forState:UIControlStateNormal];
        [delButton addTarget:self action:@selector(deletePoi:) forControlEvents:UIControlEventTouchUpInside];
        delButton.backgroundColor=[UIColor clearColor];
        delButton.frame=CGRectMake(55, 5, 30,30);

        [rightView addSubview:delButton];
        [rightView addSubview:edit];

        UIImageView *imgviewIcon=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
        [imgviewIcon setImage:[UIImage imageNamed:@"pin.png"]];
        [annotationView setImage:[UIImage imageNamed:@"pin.png"]];
        [annotationView layoutIfNeeded];
        annotationView.leftCalloutAccessoryView=imgviewIcon;
        annotationView.rightCalloutAccessoryView=rightView;

    }

    return annotationView;

}
-(IBAction)editPoi:(id)sender{
    //edit
}
-(IBAction)deletePoi:(id)sender{
    //delete
}

我正在将 NMView 添加到我的视图控制器中

NMView *nv=[[NMView alloc]initWithFrame:CGRectMake(5, 5, 300, 400)];
[self.view addSubview:nv];

现在我正在使用 NMView 的自定义方法向地图视图添加注释。
MKPointAnnotation *point=[[MKPointAnnotation alloc]init];
point.coordinate=pinCoordinate;
point.title=@"test";
[nv addCustomAnnotaion:point];

我已在iOS7中测试过,它能正常工作:

enter image description here

但在iOS8中选择标注时,整个NMView变成了空白

这是iOS 8的一个bug还是我做错了什么?请帮忙...

编辑

如果我以编程方式选择注释,则在iOS 8中也会发生相同的问题。

[mapView selectAnnotation:annot animated:YES];

其他人是否遇到过这个问题?

iOS8空白屏幕: enter image description here

我已上传项目到以下网址: https://drive.google.com/file/d/0BxkVP7smm8Z6SnM1UjBxcjc5aDg/view?usp=sharing

请检查并让我知道结果。


与您的问题无关,但如果您不设置注释视图的centerOffset,当您缩小地图时,它将显示错误的点,这在您的情况下是英文频道。 - Desdenova
@Desdenova:感谢您的回复,但现在的问题不是这个。主要问题是在选择注释时,mkmapview会变空白。 - user3057005
请查看我在http://stackoverflow.com/questions/14707080/after-json-parsing-viewforannotation-shows-only-one-single-annotation-on-mkmapv/32965056#32965056下的回答,希望对其他人有所帮助。 - blackjacx
2个回答

2

谢谢回复,按照您说的方法确实有效,但我想知道原因,为什么在iOS 7上不起作用,您能告诉我吗? - user3057005
@user3057005 如果我能的话,我很乐意解释...我猜这取决于iOS 8中的新尺寸类别或可能是一个错误,我不知道。这里有一篇更详细的文章,介绍如何将两种方法结合起来,也许对你有帮助:http://objectivetoast.com/2014/06/02/translating-autoresizing-masks-into-constraints/ - Templar

0

这不是完整的解决方案,而是诊断问题的一步。

1- 将以下内容添加到NMView类中

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {

    NSLog(@"MapView: %@", mapView);

    //so that I can check the view hierarchy after the user taps on the annotation view
    [self performSelector:@selector(viewHierarchy) withObject:nil afterDelay:5];
}

- (void) viewHierarchy {

    NSLog(@"Self: %@", self); //set a break point here
    //inspect the view hierarchy by typing 'po [self recursiveDescription]' in the debugger 

}

来自 '[po recursiveDescription]' 的输出。查看 NMView 的框架 "frame = (-65 -113; 0 0)",可能是一个自动布局问题。

NMView: 0x7fa63bc95390; frame = (-65 -113; 0 0); autoresize = W+H; layer = > | > | | ; layer = > | | | > | | | | <_MKMapLayerHostingView: 0x7fa63bc9b0e0; frame = (0 0; 0 0); layer = <_MKMapLayerHostingLayer: 0x7fa63bc9bfe0>> | | | | | (layer) | | | | | | (layer) | | | > | | | > | | | | > | | | | | > | | | | | | <_MKPopoverEmbeddingView: 0x7fa63ef600f0; frame = (149.5 155; 0 0); layer = > | | | | | | | <_UIPopoverView: 0x7fa63ef1e4e0; frame = (-84.5 -57; 170 57); layer = > | | | | | | | | > | | | | | | | | | > | | | | | | | | | | <_MKCalloutBackgroundMaskView: 0x7fa63ef1f4e0; frame = (0 0; 170 57); layer = > | | | | | | | | | | | > | | | | | | | | | | | | > | | | | | | | | | | | > | | | | | | | | | | | | > | | | | | | | | | | | | > | | | | | | | | > | | | | | | | | | > | | | | | | | | | | <_MKSmallCalloutPassthroughButton: 0x7fa63ef60900; baseClass = UIControl; frame = (0 0; 170 57); layer = > | | | | | | | | | | | <_MKCalloutAccessoryWrapperView: 0x7fa63d9f8c10; frame = (90 0; 80 50); clipsToBounds = YES; layer = > | | | | | | | | | | | | > | | | | | | | | | | | | | > | | | | | | | | | | | | | | > | | | | | | | | | | | | | > | | | | | | | | | | | | | | > | | | | | | | | | | <_MKSmallCalloutContainerView: 0x7fa63bcc4aa0; frame = (0 0; 170 44); clipsToBounds = YES; layer = > | | | | | | | | | | | <_MKCalloutAccessoryWrapperView: 0x7fa63dc6d890; frame = (7 7; 30 30); clipsToBounds = YES; layer = > |


谢谢您的回复,您能提供解决方案吗? - user3057005

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接