2014-10-03 20 views
4

我必須面對的iOS 8 我有自定義視圖(UIView的子類),它包含的MKMapView在它的MKMapView的一些奇怪的行爲空白。的MKMapView顯示在批註選擇iOS8上

NMView -> MKMapView

NMView代碼

#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的定製方法MapView類,

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

我在iOS7在其優良的工作進行了測試:

enter image description here

iOS8上上標註的選擇全NMView一片空白

是它的iOS 8的錯誤還是我做錯了什麼? 請幫助..

編輯

如果我選擇註釋編程然後也同樣的問題發生在iOS的8

[mapView selectAnnotation:annot animated:YES];

某些別有用心的人的基礎面這一問題?

iOS8上的空白屏幕。 : enter image description here

我在上傳項目如下網址: https://drive.google.com/file/d/0BxkVP7smm8Z6SnM1UjBxcjc5aDg/view?usp=sharing

請檢查它讓我知道..

+0

無關你的問題,但如果你沒有設置你的註釋視圖的'centerOffset'時,縮小時會顯示錯誤的點,在您的情況下是英文頻道。 – Desdenova 2014-10-03 08:45:24

+0

@Desdenova:謝謝你的回覆,但現在這不是問題,主要問題是mkmapview在註釋選擇上變爲空白。 – user3057005 2014-10-03 09:40:53

+0

請參閱我的anser下http://stackoverflow.com/questions/14707080/after-json-parsing-viewforannotation-shows-only-one-single-annotation-on-mkmapv/32965056#32965056希望也有助於某人。 – blackjacx 2015-10-06 08:26:01

回答

3

我不知道是什麼原因,但如果你刪除這一行它的工作原理:

self.translatesAutoresizingMaskIntoConstraints=NO; 

參考: Views disappear when `translatesAutoresizingMaskIntoConstraints` set `NO`

+0

感謝您的回覆,它按照您的說法工作,但我想知道原因,它不會在iOS 7中發生,您能否告訴我.. – user3057005 2014-10-16 10:45:20

+0

@ user3057005我很樂意解釋我是否可以......我想它取決於iOS 8中的新尺寸類別,或者可能是一個錯誤,毫無頭緒。這裏有一個關於結合這兩種方法的更詳細的帖子,也許它可以幫助你:http://objectivetoast.com/2014/06/02/translating-autoresizing-masks-into-constraints/ – Templar 2014-10-16 11:50:31

0

這不是完整的解決方案,但對診斷問題的一個步驟。

1-添加了以下的NMView類從 '[PO recursiveDescription]'

- (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 

} 

輸出。看看NMView的框架「frame = (-65 -113; 0 0)」,這可能是一個自動佈局問題。

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

+0

需要回復的問題,你能提出解決方案嗎? – user3057005 2014-10-16 10:26:59