2012-09-30 30 views
17

En iOS6, shouldAutorotateToInterfaceOrientation está en desuso. Traté de usar supportedInterfaceOrientations y shouldAutorotate para hacer que la aplicación funcione correctamente para la autorrotación, pero falló.¿Cómo hacer que la aplicación funcione correctamente para autorrotación en iOS 6?

este ViewController no quiero a girar, pero no funciona.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

-(BOOL)shouldAutorotate 
{ 
    return NO; 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

¿Alguna idea? ¡Gracias por cualquier ayuda con anticipación!

+0

¿Está el controlador de visualización incrustado dentro de un controlador de navegación o de una barra de pestañas? – Felix

+0

incrustado dentro de un controlador de navegación. @ phix23 – Carina

Respuesta

38

Lo descubrí.

1) subclase UINavigationController (la ViewController superior de la jerarquía tomará el control de la orientación.) ha configurado como self.window.rootViewController.

- (BOOL)shouldAutorotate 
{ 
    return self.topViewController.shouldAutorotate; 
} 
- (NSUInteger)supportedInterfaceOrientations 
{ 
    return self.topViewController.supportedInterfaceOrientations; 
} 

2) si usted no quiere controlador de vista gire

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

-(BOOL)shouldAutorotate 
{ 
    return NO; 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

3) si usted quiere que sea capaz de girar

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskAllButUpsideDown; 
} 

-(BOOL)shouldAutorotate 
{ 
    return YES; 
} 

Por cierto, según a sus necesidades, otro método relacionado:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 
+2

+1 para responder su propia pregunta con algunos buenos ejemplos de código –

+0

He intentado este método y funciona bien. – Robert

+0

¿Por qué sobrescribe '- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation'? Está en desuso y nunca llama al – voromax

3

Si está utilizando un controlador de barra de pestañas en lugar de un regulador de la navegación como su controlador de raíz, tendrá que subclase de manera similar UITabBarController.

también la sintaxis será diferente. Usé lo siguiente con éxito. Luego utilicé los ejemplos anteriores con éxito en los controladores de vista que quería anular. En mi caso, quería que la pantalla principal no girara, pero tenía una pantalla de preguntas frecuentes con películas que, naturalmente, quería habilitar la vista horizontal. Funcionó a la perfección! Simplemente tenga en cuenta que la sintaxis cambia a self.modalViewController (obtendrá una advertencia de compilación si intenta usar la sintaxis para un controlador de navegación). ¡Espero que esto ayude!

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (BOOL)shouldAutorotate 
{ 
    return self.modalViewController.shouldAutorotate; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return self.modalViewController.supportedInterfaceOrientations; 
} 
Cuestiones relacionadas