2016-09-05 1 views
5

Je souhaite afficher 1 ou 2 UIViewcontrollers en mode paysage tandis que d'autres dans Portrait. Pour cela j'ai implémenté cette fonction dans AppDelegate.iOS9 supportedInterfaceOrientationsForWindow arrête d'être appelé

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
    return self.orientation; 
} 

où dans AppDelegate.h, l'orientation est:

@property (nonatomic, assign) UIInterfaceOrientationMask orientation; 

Dans UIViewController (s) où je besoin d'orientation du paysage. Je place ce code

-(void)viewWillAppear:(BOOL)animated 
{ 
    self.appDelegate.orientation = UIInterfaceOrientationMaskLandscape; 
} 

et

-(void)viewWillDisappear:(BOOL)animated 
{ 
    self.appDelegate.orientation = UIInterfaceOrientationMaskPortrait; 
} 

Cependant, quand je vais à 'LandscapeViewController' cela fonctionne ok, je retourne, ça marche ok, je vais à nouveau 'LandscapeViewController' its ok, et puis quand je reviens, la méthode supportedInterfaceOrientationsForWindow cesse d'être appelée. Une raison pour cela? ou je fais quelque chose de bizarre?

Répondre

3

Si vous utilisez UITabBarController créer une classe personnalisée pour UITabBarController et placez ce code là

-(UIInterfaceOrientationMask) supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

et placez dans vos contrôleurs de vue en fonction de vos besoins.

lieu ce code dans appDelegate

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 

    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight; 

    id rootViewController = [self topViewControllerWithRootViewController:window.rootViewController]; 

    if (rootViewController != nil) 
    { 
     if ([rootViewController respondsToSelector:@selector(canRotate)]) 
     { 
      return UIInterfaceOrientationMaskLandscape; 
     } 
    } 
    return UIInterfaceOrientationMaskPortrait; 
} 

-(UIViewController*) topViewControllerWithRootViewController:(id)rootViewController 
{ 

    if (rootViewController == nil) 
    { 
     return nil; 
    } 

    if ([rootViewController isKindOfClass:[UITabBarController class]]) 
    { 
     UITabBarController *selectedTabBarController = rootViewController; 
     return [self topViewControllerWithRootViewController:selectedTabBarController.selectedViewController]; 
    } 
    else if ([rootViewController isKindOfClass:[UINavigationController class]]) 
    { 
     UINavigationController *selectedNavController = rootViewController; 
     return [self topViewControllerWithRootViewController:selectedNavController.visibleViewController]; 
    } 
    else 
    { 
     UIViewController *selectedViewController = rootViewController; 
     if (selectedViewController.presentedViewController != nil) 
     { 
      return [self topViewControllerWithRootViewController:selectedViewController.presentedViewController]; 
     } 
    } 
    return rootViewController; 
} 

et placez dans votre vue contrôleur

-(void)canRotate 
{ 

} 
+0

oui J'utilise TabBar. – Haris