Автоповорот просмотра внутри UITabBarController и UINavigationController - PullRequest
0 голосов
/ 16 июня 2009

Мой главный контроллер вида - UITabBarController, с 4 UINavigationControllers в качестве элементов панели вкладок. Каждая панель навигации представляет собой пару различных представлений, которые помещаются в ее стек.

У меня есть одно представление, которое я хочу автозапустить, но я не могу получить вызов willAnimateFirstHalfOfRotationToInterfaceOrientation в этом контроллере представления.

Я попытался создать подклассы для моих UITabBarController и UINaviationControllers и добавить переопределение для shouldAutorotateToInterfaceOrientation, например, так:

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

Даже после этого я не могу заставить вид вращаться. Я подумал, что, возможно, самый верхний UIViewController (включая вкладку и навигацию) должен иметь возможность вращаться. Или даже каждый взгляд вверх по цепочке. Я попытался переопределить shouldAutorotateToInterfaceOrientation для каждого контроллера, но все еще не могу повернуть представление.

Кто-нибудь достиг этого или есть какие-либо предложения?

Заранее спасибо!

Ответы [ 3 ]

1 голос
/ 17 ноября 2009

Вы можете использовать SubClassed UITabBarController (или использовать дополнение), который запрашивает у выбранного навигационного контроллера ответ visibleViewController на запрос поворота:

@implementation RotatingTabBarController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        if([self.selectedViewController isKindOfClass:[UINavigationController class]]){
           return [[(UINavigationController*)self.selectedViewController visibleViewController] shouldAutorotateToInterfaceOrientation:interfaceOrientation];
        } else {
           return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
        }
}
@end
1 голос
/ 16 июня 2009

Помимо создания подкласса вашего tabBarController, как вы сделали переопределение houldAutorotateToInterfaceOrientation, вы должны сделать следующее:

В методе viewDidLoad контроллера, в который вы хотите добавить вращаемое представление:

self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

Добавьте этот метод делегата в контроллер, в который вы хотите добавить вращаемое представление:

 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    //NSLog(@"didRotateFromInterfaceOrientation");


    if((fromInterfaceOrientation == UIInterfaceOrientationPortrait) ||
       (fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))
    {    

        YourAppDelegate *mainDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
    [mainDelegate.tabBarController.view addSubview: viewToBeRotated];

    [viewToBeRotated setHidden:NO];

    return;

}

if(fromInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
    [viewToBeRotated removeFromSuperview];
    [self.view setHidden:NO];
}

}

Вы также можете добавить следующий метод:

- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
                                                duration:(NSTimeInterval)duration {

if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
{
    //self.view = self.portrait;
    YourAppDelegate *mainDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
    [viewToBeRotated removeFromSuperview];
    mainDelegate.tabBarController.view.transform = CGAffineTransformIdentity;
    mainDelegate.tabBarController.view.transform = CGAffineTransformMakeRotation(degreesToRadian(0));
    mainDelegate.tabBarController.view.bounds = CGRectMake(0.0, 0.0, 300.0, 480.0);

}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
{
    YourAppDelegate *mainDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
    [mainDelegate.tabBarController.view addSubview: viewToBeRotated];
    mainDelegate.tabBarController.view.transform = CGAffineTransformIdentity;
    mainDelegate.tabBarController.view.transform = CGAffineTransformMakeRotation(degreesToRadian(-90));
    mainDelegate.tabBarController.view.bounds = CGRectMake(0.0, 0.0, 460.0, 320.0);
}
else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{


//self.view = self.portrait;
        YourAppDelegate *mainDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
        [viewToBeRotated removeFromSuperview];
        mainDelegate.tabBarController.view.transform = CGAffineTransformIdentity;
        mainDelegate.tabBarController.view.transform = CGAffineTransformMakeRotation(degreesToRadian(180));
        mainDelegate.tabBarController.view.bounds = CGRectMake(0.0, 0.0, 300.0, 480.0);
    }
    else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        YourAppDelegate *mainDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
        [mainDelegate.tabBarController.view addSubview: viewToBeRotated];
        mainDelegate.tabBarController.view.transform = CGAffineTransformIdentity;
        mainDelegate.tabBarController.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
        mainDelegate.tabBarController.view.bounds = CGRectMake(0.0, 0.0, 460.0, 320.0);
    }
}

Вы также можете взглянуть на

Поворот iPhone и создание нового UIViewController

TabBarController и навигационные контроллеры в ландшафтном режиме

0 голосов
/ 16 июня 2009

создание подкласса UITabBarController и переопределение этого метода сработало:

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

Я думаю, что моей настоящей проблемой было то, что XCode не компилировал обновленный код. Я сделал Build clean, коснулся и перезапустил XCode и симулятор, и изменения, наконец, заняли.

...