Search code examples
iosmkmapviewios8xcode6mkannotation

MKMapView show blank on annotation selection iOS8


I have face some strange behaviour of MKMapview in iOS 8. I have custom view (subclass of UIView) which contain MKMapview in it.

NMView -> MKMapView

NMView Code

#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
}

I am adding NMView to my view controller

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

Now i am adding annotation to mapview using custom method of NMView,

MKPointAnnotation *point=[[MKPointAnnotation alloc]init];
point.coordinate=pinCoordinate;
point.title=@"test";
[nv addCustomAnnotaion:point];

I have tested it in iOS7 in its working fine:

enter image description here

But in iOS8 on selection of annotation whole NMView goes blank.

Is it bug of iOS 8 or i am doing something wrong ?? Please Help..

EDIT

If i select annotation programatically then also same problem occur in iOS 8.

[mapView selectAnnotation:annot animated:YES];

Somebody else base face this issue ??

iOS8 Blank screen. : enter image description here

I have uploaded project at below url: https://drive.google.com/file/d/0BxkVP7smm8Z6SnM1UjBxcjc5aDg/view?usp=sharing

Please check it let me know..


Solution

  • I'm not exactly sure why, but it works if you delete this line :

    self.translatesAutoresizingMaskIntoConstraints=NO;
    

    Reference: Views disappear when `translatesAutoresizingMaskIntoConstraints` set `NO`