Comment puis-je faire pivoter un UIViewController à son orientation d'interface prise en charge lors de la navigation vers celui-ci?

J'ai un viewcontroller (avec des viewcontrollers enfants) qui est verrouillé en portrait, en utilisant le code suivant pour accomplir ceci:

- (UIInterfaceOrientationMask) supportedInterfaceOrientations { if ([self.centerViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) { return [self.centerViewController supportedInterfaceOrientations]; } return UIInterfaceOrientationMaskAll; } 

Cela fonctionne parfaitement, je peux "replace" les supportedInterfaceOrientations dans mon centerViewController actuel si j'ai besoin de verrouiller cette vue, et l'omettre si je veux que la vue supporte tout.

Le problème est que les vues qui sont verrouillées ne pivotent pas à leur orientation prise en charge lors de la navigation. Une vue est verrouillée en mode portrait, mais lorsque vous affichez une autre vue en mode Paysage et que vous naviguez jusqu'à celle-ci, elle s'affiche en mode Paysage, même si le mode Paysage n'est pas pris en charge pour cette vue.

Comment puis-je m'assurer que la vue est orientée selon une orientation permise lors de la navigation?

Je ferai pivoter automatiquement si vous avez returnné votre orientation supportedInterfaceOrientations dans supportedInterfaceOrientations .

 #pragma mark - Orientation Handling - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } - (BOOL)shouldAutorotate {// iOS 6 autorotation fix return YES; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations {// iOS 6 autorotation fix return UIInterfaceOrientationMaskLandscapeLeft; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {// iOS 6 autorotation fix return UIInterfaceOrientationPortrait; } - (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) { } } 

Ajouter la propriété suivante dans AppDelegate.h

 @property (readwrite) BOOL ressortingctRotation; 

Ajouter le code suivant dans AppDelegate.m

 -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if(self.ressortingctRotation) return UIInterfaceOrientationMaskLandscape; else return UIInterfaceOrientationMaskPortrait; } 

Ajoutez le code suivant dans ViewController.m dont vous voulez restreindre l'orientation:

 -(BOOL)shouldAutorotate { return NO; } -(void) ressortingctRotation:(BOOL) ressortingction { AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; appDelegate.ressortingctRotation = ressortingction; } - (NSUInteger) supportedInterfaceOrientations { // Return a bitmask of supported orientations. If you need more, // use bitwise or (see the commented return). return UIInterfaceOrientationMaskLandscape; // return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { // (iOS 6) // Prefer (force) landscape return UIInterfaceOrientationLandscapeRight; } 

Aussi quand vous partez de votre paysage ViewController.m effectuez l'appel de fonction suivant

 [self ressortingctRotation:NO]; 

J'espère que je résous votre problème.