2010-08-30 13 views
24

Duplicar posible:
iPhone orientation¿Cómo verificar la orientación del dispositivo mediante programación en iPhone?

¿Cómo comprobar la orientación del dispositivo en cualquier punto fuera de tiempo de programación en iPhone?

Gracias.

+1

Posible duplicar [orientación de iPhone] (http://stackoverflow.com/q/634745/194544) – beryllium

+0

Pruebe esto: http://jayprakashdubey.blogspot.in/2014/07/check-device-orientation.html –

+0

Puede usar un método como willTransitionToTraitCollection: withTransitionCoordinator: este método se invoca cada vez que cambia la colección de caracteres del controlador de vista. Los rasgos pueden ser las clases de tamaño horizontal y vertical. Entonces, dentro del método podemos verificar lo siguiente (en Swift): cambiar newCollection.verticalSizeClass {case .Compact: // es un caso de paisaje .Regular,.Sin especificar: // Es retrato} – plusangel

Respuesta

7

-[UIViewController interfaceOrientation]

Sin embargo, es posible recuperar la deviceOrientation, independiente de la orientación actual de la interfaz:

[[UIDevice currentDevice] orientation]

leer la documentación para obtener más información, sin embargo, usted tiene que hacer algunas cosas más para hacer que funcione

8

Proveedores:

UIInterfaceOrientationIsPortrait(orientation) 

y

UIInterfaceOrientationIsLandscape(orientation) 
69

Éstos son macros UIDeviceOrientationIsLandscape y UIDeviceOrientationIsPortrait

así que en lugar de comprobar por separado puede hacerlo de esta manera ...

if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) 
    { 
     // code for landscape orientation  
    } 

O

if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)) 
    { 
     // code for Portrait orientation  
    } 
+0

Este método funciona muy bien, pero para mí solo funciona dentro del método viewDidAppear. Estoy usando stroyboad. Es correcto ? –

+2

Esto no funciona de nuevo en iOS8 – Jacky

4

También puede comprobar dentro de la función

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 

    if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
     //do for portrait 
    }  
    else 
    { 
     //do for Landscape 
    } 
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || 
      interfaceOrientation == UIInterfaceOrientationPortrait || 
      interfaceOrientation == UIInterfaceOrientationLandscapeLeft || 
      interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown); 
} 
14

Inténtelo.

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait) ) 
{ 
    // do something for Portrait mode 
} 
else if(([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) || ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft)) 
{ 
    // do something for Landscape mode 
} 
3

Debe colocar [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; en su AppDelegate didFinishLaunchingWithOptions

Entonces, en cualquier lugar de su aplicación se puede obtener la orientación actual con:

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

y la orientación de prueba con:

UIInterfaceOrientationIsPortrait(orientation) UIInterfaceOrientationIsLandscape(orientation)

+0

Debe probar la variable 'orientation' utilizando' UIDeviceOrientationIsLandscape' y no usar 'UIInterfaceOrientationIsLandscape'. – Webdevotion

Cuestiones relacionadas