Autorotate pojedynczy UIViewController w iOS 6 z UITabBar

Mam aplikację, która działa tylko w Portrait Mode, ale jest singleView, który może wyświetlać wideo, więc chcę, aby ten widok działał również w landscape mode, ale w iOS 6 nie mogę dowiedzieć się, jak Mogę to zrobić, teraz mam to:

W AppDelegate.m mam:

self.window.rootViewController = myTabBar;

Następnie w podsumowaniu projektu:

Tutaj wpisz opis obrazka

I odkryłem, że w iOS 6 aby wykryć obrót widoku muszę to zrobić:

- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}

// Tell the system It should autorotate
- (BOOL) shouldAutorotate {
return YES;
}

Więc wstawiam powyższy kod tylko w moim UIViewController, który chcę wykorzystać również w krajobrazie, ale nie działa, ktoś wie, jak Mogę to zrobić? chcę tylko autorotate podczas pokazywania wideo.

Author: Bartłomiej Semańczyk, 2012-09-23

3 answers

Po pierwsze, ustawienia docelowe powinny wyglądać tak: Obsługiwane Orientacje Interfejsu

W UITabBarController:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // You do not need this method if you are not supporting earlier iOS Versions
    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

-(NSUInteger)supportedInterfaceOrientations
{
    if (self.selectedViewController) 
        return [self.selectedViewController supportedInterfaceOrientations];

    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate
{
    return YES;
}

Wewnątrz Twojego kontrolera ViewController:

A) jeśli nie chcesz obracać:

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

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

B) jeśli chcesz obrócić do krajobrazu:

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

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

Edit:

Innym rozwiązaniem jest zaimplementowanie tej metody wewnątrz AppDelegate:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    NSUInteger orientations = UIInterfaceOrientationMaskAll;

    if (self.window.rootViewController) {
        UIViewController* presented = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
        orientations = [presented supportedInterfaceOrientations];
    }
    return orientations; 
}
 49
Author: mientus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-20 12:19:35

Napisałbym komentarz, ale nie mogę, więc zamieszczam to jako odpowiedź.

To był mój scenariusz:

Moja aplikacja obsługuje zmianę orientacji tylko w niektórych widokach i nie mogłem dowiedzieć się, jak to zrobić tylko dla tych, których chciałem, a następnie wylądowałem na to pytanie i zobaczyłem odpowiedź mientusa (dzięki za to), a następnie poszedłem do przodu i zrobiłem to, co zasugerował, który był podklasą UITabBarController i nadpisać te metody: {]}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{

    NSLog(@"AUTO ROTATE IN CUSTOM TAB BAR");
    // You do not need this method if you are not supporting earlier iOS Versions
    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}


-(NSUInteger)supportedInterfaceOrientations{

    NSLog(@"supportedInterfaceOrientations IN CUSTOM TAB BAR");

    if (self.selectedViewController)
        return [self.selectedViewController supportedInterfaceOrientations];

    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate{

    NSLog(@"shouldAutorotate IN CUSTOM TAB BAR");
    return [self.selectedViewController shouldAutorotate];
}

Wtedy wewnątrz każdego kontrolera widoku miałbym metody wskazujące, czy chcę rotacji, czy nie. Metody w UITabBarController były wywoływane, ale nie te w moim viewcontroller dlatego rotacja nadal miała miejsce tam, gdzie nie chciałem. Następnie podklasuję UINavigationController i nadpisuję te same metody tylko z tą zmianą na supportedInterfaceOrientation one, aby wyglądały tak :

- (NSUInteger)supportedInterfaceOrientations{

NSLog(@"supportedInterfaceOrientations IN CUSTOM NAV BAR CALLING CURRENT VIEW CONTROLLER");
UIViewController* presented = [[self viewControllers] lastObject];
return [presented supportedInterfaceOrientations];

}

Co to robi w zasadzie, Pobiera bieżący kontroler widoku, a następnie pyta o obsługiwany Orientacja i voila moje metody w moim viewcontroller się nazywa i mogę obsługiwać orientację tam, gdzie chcę.

 3
Author: Oscar Salvador,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-11 17:25:33

Czy widok, który chcesz obrócić, jest podglądem podrzędnym widoku tylko portretowego? Zwykle zachowanie widoku rotacji dziedziczy z rootviewcontroller. Następnie, jeśli zwrócisz NO in shouldAutorotate w rootviewcontroller, zatrzymasz obrót w każdym widoku podrzędnym.

Proponuję podzielić architekturę w ten sposób:

RootViewController - > supportedInterfaceOrientations = Portrait & shouldAutorotate = YES NORotationViewControllers - > supportedInterfaceOrientations = Portrait & shouldAutorotate = Tak rotationViewControllers - > supportedInterfaceOrientations = All & shouldAutorotate = YES

Jeśli jeszcze tego nie czytałeś, zajrzyj na: Obsługa Wielu Orientacji Interfejsu

 0
Author: Beppe,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-23 10:48:35