2016-09-05 13 views
5

Chcę pokazać 1 lub 2 kontrolery UIView w trybie poziomym, podczas gdy inne w pionie. W tym celu zaimplementowałem tę funkcję w AppDelegate.Obsługa iOS9InterfaceOrientationsForWindow przestaje być wywoływana

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

gdzie AppDelegate.h, orientacja:

@property (nonatomic, assign) UIInterfaceOrientationMask orientation; 

W UIViewController (s), gdzie potrzeba w orientacji poziomej. I Umieść ten kod

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

i

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

Jednak, gdy idę do 'LandscapeViewController' to działa OK, wracam, to działa ok, ja znowu iść do 'LandscapeViewController' jest ok, a kiedy wrócę, metoda supportedInterfaceOrientationsForWindow przestanie być wywoływana. Jakiś powód? czy robię coś dziwnego?

Odpowiedz

3

Jeśli używasz UITabBarController utworzyć klasę niestandardową UITabBarController i umieścić ten kod tam

-(UIInterfaceOrientationMask) supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

i umieścić to w Pana zdaniem kontrolerów w zależności od potrzeb.

Umieść ten kod w 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; 
} 

i umieścić to w kontrolerze widoku

-(void)canRotate 
{ 

} 
+0

tak używam umieszczenie zakładek. – Haris

Powiązane problemy