2010-12-02 37 views
0

正如您在下面看到的,我使用calloutaccessorycontrol的座標來在我的mapview上放置另一個視圖。然而。它似乎永遠不會將控制放在calloutaccessory控制附近的任何地方。這很奇怪,因爲我正在使用它的x和y座標。正確的標註按鈕的X和Y座標不能準確地放置我的視圖

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
         calloutAccessoryControlTapped:(UIControl *)control 
{ 
    HotelInformationViewController *vc = 
    [[HotelInformationViewController alloc]initWithNibName:@"HotelInformationViewController" 
    bundle:nil control:control]; 

    [self.view addSubview:vc.view]; 
} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil control:(UIControl *)control 
{ 
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) 
    { 
    self.view.bounds = CGRectMake(control.frame.origin.x, control.frame.origin.y, 286, 286); //As you can see here I am using the x and y to place the new control 
    } 
    return self; 
} 

回答

0

您需要設置框架而不是邊界。

但另一個問題是標註按鈕的位置是相對於註釋的彈出視圖(而不是self.view)。註釋的彈出位置相對於地圖視圖等。

您需要使用convertPoint:toView:方法將標註的位置轉換爲self.view的座標系。

修改init方法如下:

- (id)initWithNibName:(NSString *)nibNameOrNil 
       bundle:(NSBundle *)nibBundleOrNil 
       control:(UIControl *)control 
      withParent:(UIView *)parentView 
{ 
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) 
    { 
     CGPoint calloutOriginInParent = 
      [control convertPoint:control.bounds.origin toView:parentView]; 

     CGRect myFrame; 
     myFrame.origin.x = 
      calloutOriginInParent.x + control.frame.size.width + 5; 
     myFrame.origin.y = calloutOriginInParent.y; 
     myFrame.size.width = 286; 
     myFrame.size.height = 286; 
     self.view.frame = myFrame; 
    } 
    return self; 
} 

在init方法上面,你也有調整酒店視圖的框架,使其不顯示關閉屏幕可能出現這種情況,如果標註靠近屏幕的右側或底部邊緣。

然後在calloutAccessoryControlTapped方法:

HotelInformationViewController *vc = [[HotelInformationViewController alloc] 
    initWithNibName:@"HotelInformationViewController" 
    bundle:nil 
    control:control 
    withParent:self.view]; 

[self.view addSubview:vc.view]; 

[vc release]; 
相關問題