la définition par programme de l'orientation dans viewdidload ne fonctionne pas
-
23-12-2019 - |
Question
J'ai créé une application pour iPad dans laquelle l'écran d'accueil doit être affiché uniquement en mode portrait.J'ai donc utilisé le code ci-dessous dans viewdidload.
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];
Mais ça ne fonctionne pas.J'utilise iOS 7 avec Xcode 5.Si j'ouvre mon application en mode paysage, elle devrait automatiquement passer en mode portrait.mais j'obtiens ceci :
mais ça devrait être comme ça :
quelqu'un peut-il m'aider à résoudre ce problème.Merci d'avance.
La solution
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];
Cette méthode est obsolète.Vous ne pouvez plus utiliser cette méthode.
https://stackoverflow.com/a/12813644/1405008
ci-dessus Link donne des détails sur la manière de fournir uniquement le mode portrait de UIViewController
.
Si vous avez poussé dans NavigationViewController
, essayez ceci.
Autres conseils
Le code suivant est sur le point de transformer une vue de navigationController de Portrait sur Paysage Programmatiquement:
[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;
}
J'espère simplement vous fournir d'autres idées.
Copier ce code dans votre 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 Selon votre exigence
de vos captures d'écran Je peux voir que votre application est une base d'onglets.Quelques jours plus tôt, je suis confronté au même problème.Je l'ai résolu.J'ai posé une question presque la même chose à votre.Puis, après quelques heures de lecture, j'ai résolu le problème.Il y a le lien vers mon question et réponse .
Il y a beaucoup de questions et réponses similaires sur SO, mais d'une manière ou d'une autre, aucune d'entre elles n'a fonctionné pour moi, car je devais le permettre uniquement le mode portrait sur iPhone, et uniquement le mode paysage sur iPad, alors je partage ma solution :
Supprimez toutes les méthodes liées à l'orientation de l'appareil (
shouldAutoRotate, supportedInterfaceOrientations, preferredInterfaceOrientations
, etc.) car ils pourraient être en conflit les uns avec les autresDans les paramètres du projet, activez les 4 modes :Portrait, À l'envers, Paysage à gauche, Paysage à droite
Choisissez une méthode d'identification du type d'appareil (iPhone ou iPad).j'utilise
UIScreen.mainScreen().bounds.height which
n'est pas idéal mais répond à mes besoinsPlacez ce qui suit dans 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; } }
Testé sur Xcode 7.1.1 tous les simulateurs iPhone et iPad exécutant iOS9.1