2010-02-22 15 views
6

Estoy tratando de cambiar el tamaño de los objetos en una UIView cuando se gira el dispositivo sin una codificación dura del ancho y alto. En este código, ¿cómo obtendría el newWidth y el newHeight?De willAnimateRotationToInterfaceOrientation ¿cómo puedo obtener el tamaño de la vista después de la rotación?

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

Respuesta

10

Esto sería lo correcto.

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

¿Qué sucede si estás en Landscape rotating to Portrait? – progrmr

+1

applicationFrame siempre se establece en las dimensiones de retrato. Por lo tanto, solo puede voltearlos si el dispositivo va a una orientación no vertical. – Kalle

+0

No es exactamente lo que estaba buscando, pero esto lo haría. – respectTheCode

0

Podría ser mejor para crear una vista separada por completo y llamar a ese punto de vista en el método hicieron rotar.

1

Si es posible, es mejor, ya sea:

  1. subclases UIView y hacer la distribución deseada en el interior -(void)layoutSubviews, o;
  2. Haciendo uso de autoresizingMask para distribuir automáticamente sus vistas.
0

Puede configurar las propiedades de Autosizing de la vista desde el constructor de la interfaz. Eso cambiará el tamaño de su vista cuando ocurra la rotación. Pero puede haber un problema de que algunas de las vistas no se ajusten correctamente.

Cuestiones relacionadas