Iphone UITabBarController和UINavigationController内部的自动旋转视图

Iphone UITabBarController和UINavigationController内部的自动旋转视图,iphone,uiviewcontroller,uinavigationcontroller,uitabbarcontroller,Iphone,Uiviewcontroller,Uinavigationcontroller,Uitabbarcontroller,我的主视图控制器是一个uitabarcontroller,其中4个UINavigationControllers作为选项卡栏项。每个导航栏都是几个不同的视图,它们被推到它的堆栈上 我有一个想要自动旋转的视图,但我无法在该视图控制器中调用WillAnimateFirstalFoFrotationInterfaceOrientation 我已尝试将我的UITabBarController和UINaviationControllers子类化,并为shouldAutorotateToInterfaceO

我的主视图控制器是一个
uitabarcontroller
,其中4个
UINavigationControllers
作为选项卡栏项。每个导航栏都是几个不同的视图,它们被推到它的堆栈上

我有一个想要自动旋转的视图,但我无法在该视图控制器中调用
WillAnimateFirstalFoFrotationInterfaceOrientation

我已尝试将我的
UITabBarController
UINaviationControllers
子类化,并为
shouldAutorotateToInterfaceOrientation
添加覆盖,如下所示:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}
即使在那之后,我也无法使视图旋转。我想也许最顶端的
UIViewController
(包括选项卡和导航)必须能够旋转。甚至是链条上的每个视图。我已尝试为每个控制器覆盖
shouldAutorotateToInterfaceOrientation
,但仍然无法使视图旋转

有人做到了这一点或者有什么建议吗


提前谢谢

除了像覆盖houldAutorotateToInterfaceOrientation那样对tabBarController进行子类化之外,还必须执行以下操作:

在要在其中添加要旋转的视图的控制器的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];
}
在要在其中添加要旋转的视图的控制器中添加此委托方法:

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);
    }
}
您可能还想看看


将UITabBarController子类化并重写此方法有效:

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

我认为我真正的问题是XCode没有编译更新的代码。我确实创建了清理、触摸并重新启动了XCode和模拟器,最终进行了更改。

您可以使用子类的
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

我实际上交换了两个视图。我试着设置自动重设屏幕,但还是不起作用。你建议的另外两种方法应该由my-WillAnimateFirsthalfofrotationInterfaceOrientation方法处理。它根据方向交换了self.view。这个方法甚至从来没有被调用过。我想这会让所有的视图控制器都可以旋转。哎呀,现在才知道这是一个老问题。苹果公司不鼓励将UITabBarController子类化。