2010-12-02 1 views
0

Comme vous pouvez le voir ci-dessous, j'utilise les coordonnées de calloutaccessorycontrol pour placer une autre vue sur ma mapview. Toutefois. il ne semble jamais placer le contrôle n'importe où près du calloutaccessorycontrol. C'est étrange, car j'utilise ses coordonnées x et y.Les coordonnées X et Y du bouton de légende droit ne placent pas mes vues avec précision

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

Répondre

0

Vous devez définir le cadre au lieu des limites.

Mais l'autre problème est que la position du bouton de légende est relative à la vue popup de l'annotation (et non à self.view). La position contextuelle de l'annotation est relative à l'affichage de la carte, etc.

Vous devez convertir la position de la légende en système de coordonnées self.view à l'aide de la méthode convertPoint:toView:.

Modifier la méthode d'initialisation comme suit:

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

Dans la méthode d'initialisation ci-dessus, vous devez également régler le cadre de la vue de l'hôtel afin qu'il ne se présente pas hors de l'écran qui pourrait se produire si le La légende est proche du bord droit ou inférieur de l'écran.

Puis, dans la méthode calloutAccessoryControlTapped:

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

[self.view addSubview:vc.view]; 

[vc release]; 
Questions connexes