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.
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.
-[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
Proveedores:
UIInterfaceOrientationIsPortrait(orientation)
y
UIInterfaceOrientationIsLandscape(orientation)
É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
}
Este método funciona muy bien, pero para mí solo funciona dentro del método viewDidAppear. Estoy usando stroyboad. Es correcto ? –
Esto no funciona de nuevo en iOS8 – Jacky
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);
}
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
}
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)
Debe probar la variable 'orientation' utilizando' UIDeviceOrientationIsLandscape' y no usar 'UIInterfaceOrientationIsLandscape'. – Webdevotion
Posible duplicar [orientación de iPhone] (http://stackoverflow.com/q/634745/194544) – beryllium
Pruebe esto: http://jayprakashdubey.blogspot.in/2014/07/check-device-orientation.html –
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