2013-12-12 13 views
7

Moja aplikacja to UITabBarController -> UINavigationController -> UITableViewController -> UIViewController.Jak ograniczyć kontroler orientacji na widok w iOS7 w hierarchii kontrolera nawigacji?

chcę zrobić 2 rzeczy:

  1. zapobiec wyświetlaniu mojej tableview obracaniu, chcę, aby zawsze pozostać portret.

  2. SIŁA & Pozwól mojemu UIViewcontrollerowi na obracanie pola krajobrazowego.

Co wiem:

  1. Rozumiem, że viewcontroller na szczycie hierarchii kontroluje obrót. To byłby mój UITabBarController? Lub raczej jego jedyny viewcontroller, który byłby na objectIndex: 0?

  2. Moje ustawienia projektu pozwalają na rotację portretów, LL i LR. Myślę, że to wzór, który muszę wykonać, aby rozwiązać ten problem, to pozwolić ALL na najwyższym poziomie na obracanie się, a następnie kontrolowanie każdego pojedynczego vc indywidualnie, prawda?

To jest to, co do tej pory znalazłem w SO.

Tak więc dla mojej najwyższej hierarchii ustawiam ustawienia projektu, aby umożliwić obrót portretu, LL i LR.

a następnie w moim tableviewcontroller którego nie chcę, aby obrócić:

-(BOOL)shouldAutorotate{ 
    return NO; 
} 

-(NSUInteger)supportedInterfaceOrientations{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

i wreszcie w moim UIViewController, które chcę, aby obrócić:

-(NSUInteger)supportedInterfaceOrientations{ 
    return UIInterfaceOrientationMaskAllButUpsideDown; 
} 

-(BOOL)shouldAutorotate{ 
    return YES; 
} 

Jednak to nie działa. Mogę obracać oba w dowolnym kierunku. Nie wiem też, jak wymusić obrót LL, kiedy dojdę do mojego uivc, który jest wywoływany z modalnego segue z mojego tablevc.

Każda pomoc w zrozumieniu tego bałaganu byłaby ogromnie doceniona :)

Odpowiedz

6

OK, oto jest. trochę skomplikowane. Ustawienia

projektowe muszą umożliwiać P, LL & LR

Storyboard jest UINavController z UITableViewController z przyciskiem bar Push segue do UIViewController.

Wszystkie sceny scenorysu musiały być interpretowane jako orientacja w symulowanych metrykach. Po prostu mówię, bo po chwili miałem je wszystkie z różnymi ustawieniami po testowaniu tak wiele.

Musi mieć klasę dla NavController, TableViewController i podaną UIVController. Moja aplikacja rozpoczęła się jako widok pojedynczy, a następnie przeciągnąłem UITVC i ostatecznie utworzyłem UITVC w UINVC. Następnie podłączyłem UIVC do elementu przycisku paska UITVC, który przeciągnąłem za pomocą funkcji push push.

Set aplikacje window.rootvc do navVC, twój top vc hierarchia (nie zapomnij, aby ustawić ten identyfikator w storyboardów czy będzie to awarię oczywiście):

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil]; 
    UINavigationController *myNavC = (UINavigationController*)[mainStoryboard instantiateViewControllerWithIdentifier:@"MainNav"]; 
    self.window.rootViewController = myNavC; 

Powiedz Big Boss UINVC każdy może obracać:

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

    - (UIInterfaceOrientationMask)supportedInterfaceOrientations { 
      return self.topViewController.supportedInterfaceOrientations;  
    } 

Ograniczanie tablevc więc nie będzie się obracać:

- (BOOL)shouldAutorotate { return NO; } 

    - (UIInterfaceOrientationMask)supportedInterfaceOrientations { 
     return (UIInterfaceOrientationMaskPortrait); 
    } 

Pozwól, aby ostatnie UIVC się obróciło:

- (BOOL)shouldAutorotate { return YES; } 

    - (UIInterfaceOrientationMask)supportedInterfaceOrientations { 
     return (UIInterfaceOrientationMaskAllButUpsideDown);  
    } 
+2

ta działa. Ale frustrujące jest to, że XCode/IB sam tego nie rozumie i "po prostu działa". Głupia rotacja. :/ –

12

Proste, ale działa bardzo dobrze. IOS 7,1 i 8

AppDelegate.h

@property() BOOL restrictRotation; 

AppDelegate.m

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

ViewController

-(void) restrictRotation:(BOOL) restriction 
{ 
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; 
    appDelegate.restrictRotation = restriction; 
} 

viewDidLoad

[self restrictRotation:YES]; or NO 
+0

Ze wszystkich obejść to działa tylko dla mnie .. Dzięki. –

+0

Najprostsze, najłatwiejsze w obsłudze rozwiązanie –

+0

Jeśli jest używany w kontrolerze nawigacyjnym, najlepiej jest go używać w viewWillAppear ... Dobra odpowiedź brzmi .. – geekyaleks

0

responce

Nie należy mylić UIDeviceOrientation i interfejsu orientację tu jest moje rozwiązanie

Appdelegate.h

@property() UIInterfaceOrientationMask restrictRotation; 

AppDelegate.m

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
    UIInterfaceOrientationMask restrictionOrientation = 0; 

if (_restrictRotation == 0) 
    return UIInterfaceOrientationMaskAll; 

UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation]; 

switch (currentOrientation) 
{ 
    case UIDeviceOrientationPortrait: 

     if (!(UIInterfaceOrientationMaskPortrait & _restrictRotation)) 
      restrictionOrientation = UIInterfaceOrientationMaskAll; 
     else 
      restrictionOrientation = UIInterfaceOrientationMaskPortrait; 

     break; 

    case UIDeviceOrientationPortraitUpsideDown: 

     if (!(UIInterfaceOrientationMaskPortraitUpsideDown & _restrictRotation)) 
      restrictionOrientation = UIInterfaceOrientationMaskAll; 
     else 
      restrictionOrientation = UIInterfaceOrientationMaskPortrait; 

     break; 

    case UIDeviceOrientationLandscapeLeft: 

     if (!(UIInterfaceOrientationMaskLandscapeLeft & _restrictRotation)) 
      restrictionOrientation = UIInterfaceOrientationMaskAll; 
     else 
      restrictionOrientation = UIInterfaceOrientationMaskPortrait; 

     break; 

    case UIDeviceOrientationLandscapeRight: 

     if (!(UIInterfaceOrientationMaskLandscapeRight & _restrictRotation)) 
      restrictionOrientation = UIInterfaceOrientationMaskAll; 
     else 
      restrictionOrientation = UIInterfaceOrientationMaskPortrait; 

     break; 

    case UIDeviceOrientationUnknown: 

     if (!(UIInterfaceOrientationUnknown & _restrictRotation)) 
      restrictionOrientation = UIInterfaceOrientationMaskAll; 
     else 
      restrictionOrientation = UIInterfaceOrientationMaskPortrait; 

     break; 

    default: 

     NSLog(@"Unknown orientation"); 

     break; 
} 

return restrictionOrientation; 

}

W każdy vc dodaje funkcję jon

-(void) restrictRotation:(UIInterfaceOrientationMask) restriction 
{ 
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; 
    appDelegate.restrictRotation = restriction; 

}

viewDidAppear

// 
// Set vc orientation 
// 

[self restrictRotation:UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight]; 
or what you want. 

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:@"orientation"]; 

or what you want. 
Powiązane problemy