definir programaticamente a orientação no viewdidload não está funcionando
-
23-12-2019 - |
Pergunta
Criei um aplicativo para ipad no qual a tela inicial deve ser exibida apenas no modo retrato.Então, usei o código abaixo no viewdidload.
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];
Mas isto não está funcionando.Estou usando iOS 7 com xcode 5.Se eu abrir meu aplicativo no modo paisagem, ele deverá mudar automaticamente para o modo retrato.mas estou ficando assim:
mas deveria ser assim:
alguém pode me ajudar a resolver esse problema.Desde já, obrigado.
Solução
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];
Este método está obsoleto.Você não pode mais usar este método.
https://stackoverflow.com/a/12813644/1405008
O link acima fornece detalhes sobre como fornecer o modo somente retrato para UIViewController
.
Se você empurrou para dentro NavigationViewController
então tente isso.
Outras dicas
O código a seguir está prestes a transformar uma visualização navigationController de Retrato para Paisagem programaticamente:
[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;
}
Só espero fornecer algumas outras idéias.
copie este código no seu 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;
}
}
defina BOOL de acordo com sua necessidade
Pelas capturas de tela, posso ver que seu aplicativo é uma base de guias.Poucos dias antes, enfrentei o mesmo problema.Eu resolvi isso.Eu fiz uma pergunta quase igual à sua.Então, depois de algumas horas de leitura, resolvi o problema.Tem o link do meu pergunta e resposta.
Existem muitas perguntas e respostas semelhantes no SO, mas de alguma forma nenhuma delas funcionou para mim, pois eu precisava permitir apenas modo retrato no iPhone, e apenas modo paisagem no iPad, então compartilhando minha solução:
Remova todos os métodos relacionados à orientação do dispositivo (
shouldAutoRotate, supportedInterfaceOrientations, preferredInterfaceOrientations
, etc), pois eles podem estar em conflito entre siNas configurações do projeto, habilite todos os 4 modos:Retrato, de cabeça para baixo, paisagem à esquerda, paisagem à direita
Escolha um método para identificar o tipo de dispositivo (iPhone ou iPad).estou usando
UIScreen.mainScreen().bounds.height which
não é ideal, mas atende às minhas necessidadesColoque o seguinte no 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; } }
Testado no Xcode 7.1.1 todos os simuladores de iPhone e iPad rodando iOS9.1