configuración mediante programación de la orientación en el viewdidload no está funcionando
-
23-12-2019 - |
Pregunta
He creado una app para ipad en la que la pantalla debe mostrar sólo en el modo de retrato.Así que he utilizado el código de abajo en viewdidload.
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];
pero no es trabajo.Estoy usando ios 7 con xcode 5.Si abro la app de modo horizontal es shouuld automáticamente para el modo de retrato.pero me estoy poniendo como este:
pero debe ser como esto:
puede alguien ayudarme a resolver este problema.Gracias de antemano.
Solución
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];
Este método está en desuso.Usted no puede utilizar este método ya.
https://stackoverflow.com/a/12813644/1405008
Enlace de arriba da detalles sobre cómo proporcionar retrato único modo para UIViewController
.
Si usted empuja en NavigationViewController
a continuación, intente esto.
Otros consejos
El siguiente código está a punto de transformar una vista de Navegación de Navegación desde Retrato a Paisaje programáticamente:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
self.navigationController.view.transform = CGAffineTransformIdentity;
self.navigationController.view.transform = CGAffineTransformMakeRotation(M_PI*(90)/180.0);
self.navigationController.view.bounds = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
[UIView commitAnimations];
self.view.frame = self.view.frame; //This is necessary
self.wantsFullScreenLayout = YES;
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeRight;
}
Solo espero proporcionarle algunas otras ideas.
Copie este código en su ViewDidload
UIInterfaceOrientation orientation = (UIInterfaceOrientation)[[UIDevice currentDevice] orientation];
if(orientation == UIInterfaceOrientationPortrait)
{
isLandscapeMode = NO;
inLandscapeRight = NO;
inLandscapeLeft = NO;
}
else if(orientation == UIInterfaceOrientationLandscapeRight)
{
isLandscapeMode = YES;
inLandscapeRight = YES;
inLandscapeLeft = NO;
} else if(orientation == UIInterfaceOrientationLandscapeLeft)
{
isLandscapeMode = YES;
inLandscapeRight = NO;
inLandscapeLeft = YES;
}
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
{
isLandscapeMode = YES;
inLandscapeLeft = YES;
inLandscapeRight = NO;
frameForProfile = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
isLandscapeMode = YES;
inLandscapeLeft = NO;
inLandscapeRight = YES;
frameForProfile = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
}
else
{
isLandscapeMode = NO;
inLandscapeLeft = NO;
inLandscapeRight = NO;
}
}
Configurar BOOL DESDE SU REQUISITO
de usted Capturas de pantalla Puedo ver que su aplicación es la base de la pestaña.Pocos días antes me enfrenté al mismo problema.Lo resolví.Hice una pregunta casi igual a tu.Luego, después de unas horas de lectura, resolví el problema.Allí está el enlace a mi pregunta y respuesta .
Hay muchas preguntas y respuestas similares, pero de alguna manera ninguna de ellas funcionó para mí, ya que necesitaba permitirlo solo el modo de retrato en iPhone , y solo el modo de paisaje solo eniPad , por lo que compartir mi solución:
-
Eliminar todos los métodos relacionados con la orientación del dispositivo (
shouldAutoRotate, supportedInterfaceOrientations, preferredInterfaceOrientations
, etc.), ya que pueden estar en conflicto entre sí -
En la configuración del proyecto Habilitar los 4 modos: retrato, boca abajo, paisaje a la izquierda, paisaje derecho
-
Elija un método para identificar el tipo de dispositivo (iPhone o iPad).Estoy usando
UIScreen.mainScreen().bounds.height which
no es ideal, pero se adapta a mis necesidades -
Coloque lo siguiente en AppDelegate
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask { if UIScreen.mainScreen().bounds.height < 760 { //iPhone UIApplication.sharedApplication().setStatusBarOrientation(.Portrait, animated: false); return UIInterfaceOrientationMask.Portrait; } else { UIApplication.sharedApplication().setStatusBarOrientation(.LandscapeLeft, animated: false); return UIInterfaceOrientationMask.Landscape; } }
probado en Xcode 7.1.1 Todos los simuladores de iPhone y iPad que ejecutan iOS9.1