l'impostazione dell'orientamento a livello di codice in viewdidload non funziona
-
23-12-2019 - |
Domanda
Ho creato un'app per iPad in cui la schermata iniziale dovrebbe essere visualizzata solo in modalità verticale.Quindi ho usato il codice seguente in viewdidload.
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];
ma non sta funzionando.Sto usando ios 7 con xcode 5.Se apro la mia app dalla modalità orizzontale, dovrebbe passare automaticamente alla modalità verticale.ma mi viene così:
ma dovrebbe essere così:
qualcuno può aiutarmi a risolvere questo problema.Grazie in anticipo.
Soluzione
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];
.
Questo metodo è deprecato.Non è più possibile utilizzare questo metodo.
https://stackoverflow.com/a/12813644/1405008
Sopra il collegamento dà dettagli su come fornire solo la modalità di ritratto per UIViewController
.
Se hai inserito in NavigationViewController
, prova questo.
Altri suggerimenti
Il codice successivo è per trasformare una vista di navigazioneController dal ritratto al paesaggio a livello programmatico:
[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;
}
.
Basta spero di fornirti altre idee.
Copia questo codice nel tuo 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;
}
}
.
Set bool in base alle tue esigenze
Da te screenshots posso vedere che la tua app è la base di tab.Pochi giorni prima ho affrontato lo stesso problema.L'ho risolto.Ho fatto una domanda quasi uguale al tuo.Poi dopo alcune ore di lettura ho risolto il problema.C'è il link al mio Domanda e risposta .
Ci sono molte domande e risposte simili su SO, ma in qualche modo nessuna di queste ha funzionato per me, come dovevo consentirlo solo modalità ritratto su iPhone, E solo modalità orizzontale su iPad, quindi condivido la mia soluzione:
Rimuovere tutti i metodi relativi all'orientamento del dispositivo (
shouldAutoRotate, supportedInterfaceOrientations, preferredInterfaceOrientations
, ecc.) poiché potrebbero essere in conflitto tra loroNelle impostazioni del progetto abilita tutte e 4 le modalità:Ritratto, Sottosopra, Orizzontale a sinistra, Orizzontale a destra
Scegli un metodo per identificare il tipo di dispositivo (iPhone o iPad).sto usando
UIScreen.mainScreen().bounds.height which
non è l'ideale ma soddisfa le mie esigenzeInserisci quanto segue in 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; } }
Testati su Xcode 7.1.1 tutti i simulatori di iPhone e iPad con iOS9.1