2010-02-22 9 views
6

Sto provando a ridimensionare gli oggetti in un UIView quando il dispositivo viene ruotato senza codificare hard la larghezza e l'altezza. In questo codice come otterrei il newWidth e il newHeight?Da willAnimateRotationToInterfaceOrientation come posso ottenere la dimensione della vista dopo la rotazione?

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
    child.frame = CGRectMake(10, 10, newWidth - 20, newHeight - 20); 
} 

risposta

10

Questo sarebbe giusto.

- (void)willAnimateRotationToInterfaceOrientation: 
    (UIInterfaceOrientation)toInterfaceOrientation 
    duration:(NSTimeInterval)duration 
{ 
    // we grab the screen frame first off; these are always 
    // in portrait mode 
    CGRect bounds = [[UIScreen mainScreen] applicationFrame]; 
    CGSize size = bounds.size; 

    // let's figure out if width/height must be swapped 
    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) { 
     // we're going to landscape, which means we gotta swap them 
     size.width = bounds.size.height; 
     size.height = bounds.size.width; 
    } 
    // size is now the width and height that we will have after the rotation 
    NSLog(@"size: w:%f h:%f", size.width, size.height); 
} 
+0

Che cosa succede se si è in Paesaggio ruotando su Ritratto? – progrmr

+1

applicationFrame è sempre impostato sulle dimensioni per verticale. Quindi li capovolgi solo se il dispositivo sta per orientamento non verticale. – Kalle

+0

Non esattamente quello che stavo cercando ma questo lo farebbe. – respectTheCode

0

Potrebbe essere meglio creare una vista separata e chiamare quella vista nel metodo di rotazione.

1

Se possibile, è meglio uno:

  1. Subclassing UIView e fare la configurazione desiderata all'interno -(void)layoutSubviews, o;
  2. Fare uso di autoresizingMask per disporre automaticamente le viste.
0

È possibile impostare le proprietà di autosizing della vista dal generatore di interfacce. Questo ridimensionerà la tua vista quando avviene la rotazione. Ma potrebbe esserci il problema che alcune delle viste potrebbero non adattarsi correttamente.

Problemi correlati